views:

82

answers:

2

This is a fully functional WCF Hello World program. I.e. I am able to run this program without any Exception.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;

namespace DataContractsNamespace
{
    [DataContract]
    public class AccountInfo
    {
        [DataMember]
        public string FirstName { get; set; }

        [DataMember]
        public string LastName { get; set; }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace Clients
{
    public class BankProxy : ServiceContractsNamespace.IBank
    {
        ServiceContractsNamespace.IBank channel;

        public BankProxy()
        {
            channel = ChannelFactory<ServiceContractsNamespace.IBank>.CreateChannel(new BasicHttpBinding(), new EndpointAddress("http://localhost:8000/Services/BankService"));
        }

        public decimal GetAcccountBalance(string AcctNo)
        {
           return channel.GetAcccountBalance(AcctNo);
        }

        public DataContractsNamespace.AccountInfo GetAccountInfo(string AcctNo)
        {
             return channel.GetAccountInfo(AcctNo);
        }
    }
}

using System;
using System.Collections.Generic;
using System.ServiceModel;
using System.Text;

namespace ServiceContractsNamespace
{
    [ServiceContract]
    public interface IBank
    {
        [OperationContract]
        decimal GetAcccountBalance(string AcctNo);

        [OperationContract]
        DataContractsNamespace.AccountInfo GetAccountInfo(string AcctNo);
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Clients
{
    class Program
    {
        static void Main(string[] args)
        {
            BankProxy prox = new BankProxy();
            Console.WriteLine("Hit enter to invoke the service call. Type exit then enter to close");

            while (Console.ReadLine() != "exit")
            {
                string balance = prox.GetAcccountBalance("1234").ToString("c");
                DataContractsNamespace.AccountInfo ai = prox.GetAccountInfo("1234");
                Console.WriteLine("{0} {1} your account balance is {2}.", ai.FirstName, ai.LastName, balance);
            }
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Hosts
{
    public class BankService : ServiceContractsNamespace.IBank
    {
        public decimal GetAcccountBalance(string AcctNo)
        {
            return 1.37m;
        }

        public DataContractsNamespace.AccountInfo GetAccountInfo(string AcctNo)
        {
            DataContractsNamespace.AccountInfo ai = new DataContractsNamespace.AccountInfo();
            ai.FirstName = "Paul";
            ai.LastName = "Johansen";
            return ai;
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;

namespace Hosts
{
    class Program
    {
        static void Main(string[] args)
        {
            ServiceHost servHo = new ServiceHost(typeof(BankService), new Uri("http://localhost:8000/Services"));
            servHo.AddServiceEndpoint(typeof(ServiceContractsNamespace.IBank), new BasicHttpBinding(), "BankService");

            servHo.Open();
            Console.WriteLine("This service is open for business. Hit Enter to close.");
            Console.ReadLine();
            servHo.Close();
        }
    }
}

As you can see, AccountInfo - Data contract is shared by both Client and Host.

I need to keep data contract only to Host/Service side.

Clients should only see interfaces of DataContracts (like IAccountInfo).

How should I modify my program to introduce IAccountInfo?

A: 

And what should IBank.GetAccountInfo return to client if you don't want to share AccountInfo? create 2 classes make the first datacontract the second not, and where you want to share use the first one, where not, the second one

ArsenMkrt
IBank.GetAccountInfo would return IAccountInfo.
JMSA
and what? should be a class in client side that implements IAccountInfo? why don't you want to create 2 classes?
ArsenMkrt
+1  A: 

It sounds like you want to return an interface instead of a class. I'm not exactly sure why you are not content to return AccountInfo. However, you should be able to do this but you will need to use a KnownType or perhaps ServiceKnownType to make it work.

Alternately, if you are working in a fully .NET environment you can use the NetDataContractSerializer instead of the DataContractSerializer.

For reference and examples you can check out:

Tuzo