2
Answers

WCF Reference

loukil sana

loukil sana

11y
1.1k
1
Hi, how i can create the WCF Reference in a silverlight2 application with c#
Answers (2)
0
Raj Kumar

Raj Kumar

NA 36.4k 16.4m 11y
Start by createing a basic class library project and reference the System.ServiceModel. This library is going to be referenced by both the client and the server. Don't use the WCF templates as they will only make your life harder on the long run.

Add your service interface to the newly created project:

[ServiceContract(Name = "TestService", Namespace = "http://www.company.com/tests")]
public interface ITestService{
    
    [OperationContract]
    TestData GetData(TestData data);

}

[DataContract]
public class TestData
{
    public TestData(string message)
    {
        Message = message;
    }

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

    public override string ToString()
    {
        return "Some shared override";
    }
}

Now add a new project to your solution and implement the server the same way you normaly would:

public class TestService: ITestService
{
    #region ITestService Members

    public TestData GetData(TestData data)
    {
        return new TestData("Hello world! " + data.Message);
    }

    #endregion
}

Instead of messing around with the XML configuration file, just configure your host by code:

ServiceHost host = new ServiceHost(typeof(TestService), new Uri[] { new Uri(uri) });
NetTcpBinding binding = new NetTcpBinding(SecurityMode.Transport);

binding.TransferMode = TransferMode.Streamed;
binding.MaxBufferSize = 65536;
binding.MaxReceivedMessageSize = 104857600;
host.AddServiceEndpoint(typeof(ITestService), binding, "service1?);

host.Open();

As you can see there is not much new to implementing the service, but now it's time for the client!

Create a new project for the client and reference the class library you first created. Instead of going for the "Add Service Reference" option create your own proxy:

public class TestServiceClient: ITestService
{

    private ITestService service;
    
    public TestServiceClient(Uri uri)
    {
        // Any channel setup code goes here
        EndpointAddress address = new EndpointAddress(uri);
        NetTcpBinding binding = new NetTcpBinding(SecurityMode.Transport);
        binding.TransferMode = TransferMode.Streamed;
        binding.MaxBufferSize = 65536;
        binding.MaxReceivedMessageSize = 104857600;

        ChannelFactory<ITestService> factory = new ChannelFactory<ITestService>(binding, address);
        service = factory.CreateChannel();
    }

    #region ITestService Members

    public TestData GetData(TestData data)
    {
        return service.GetData(data);
    }

    #endregion
}
0
Jignesh Trivedi

Jignesh Trivedi

NA 61k 14.2m 11y
hi,

Please refer below link it might help you.
http://msdn.microsoft.com/en-us/magazine/cc794260.aspx