0
votes

I am learning about how to do worker role communication using WCF (Windows Communication Foundation).

I exactly followed the demo shown at this link: Worker Role Communication

But when I the program, I am getting an InvalidOperation Exception:

The contract name 'WcfServiceLibrary1.IService1' could not be found in the list of contracts implemented by the service 'System.ServiceModel.ServiceHost'.

Here is my Stack Trace:

Unhandled Exception: System.InvalidOperationException: The contract name 'WcfServiceLibrary1.IService1' could not be found in the list of contracts implemented by the service 'System.ServiceModel.ServiceHost'. at System.ServiceModel.ServiceHost.ValidateContractType(Type implementedContract, ReflectedAndBehaviorContractCollection reflectedAndBehaviorContracts) at System.ServiceModel.ServiceHost.AddServiceEndpoint(Type implementedContract, Binding binding, Uri address, Uri listenUri)
at System.ServiceModel.ServiceHost.AddServiceEndpoint(Type implementedContract, Binding binding, Uri address) at System.ServiceModel.ServiceHost.AddServiceEndpoint(Type implementedContract, Binding binding, String address, Uri listenUri)
at System.ServiceModel.ServiceHost.AddServiceEndpoint(Type implementedContract, Binding binding, String address) at WorkerRole1.WorkerRole.StartService1() in C:\Users\veda\documents\visual studio 2010\Projects\roleToRoleCommInternalEndPt\WorkerRole1\WorkerRole.cs:line 53 at WorkerRole1.WorkerRole.Run() in C:\Users\veda\documents\visual studio 2010\Projects\roleToRoleCommInternalEndPt\WorkerRole1\WorkerRole.cs:line 26 at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.StartRoleInternal() at Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.StartRole()
at Microsoft.WindowsAzure.ServiceRuntime.Implementation.Loader.RoleRuntimeBridge.b__1() at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

The Code inside the worker role:

private void StartService1()
    {
        Trace.TraceInformation("Starting service 1 host...");

        this.serviceHost = new ServiceHost(typeof(ServiceHost));
        var binding = new NetTcpBinding(SecurityMode.None);

        serviceHost.AddServiceEndpoint(typeof(WcfServiceLibrary1.IService1),
                                        binding,
                                        string.Format("net.tcp://{0}/Service1",
                                        RoleEnvironment.CurrentRoleInstance.InstanceEndpoints["RoleToRole"].IPEndpoint));

        WorkerRole.factory = new ChannelFactory<IService1>(binding);

        try
        {
            serviceHost.Open();
            Trace.TraceInformation("Service Host started successfully.");
        }
        catch (Exception e)
        {
            Trace.TraceError("There is an error {0}:", e.Message);
        }                               
    }

The WCF code:

    namespace WcfServiceLibrary1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string SayHello(string value);


    }
}

Can anyone tell me how to solve this problem.

1

1 Answers

0
votes

Your first problem is that you told ServiceHost to host ServiceHost, instead of your service class. This line doesn't make any sense:

this.serviceHost = new ServiceHost(typeof(ServiceHost));  

You need to replace it with:

this.serviceHost = new ServiceHost(typeof(Service1)); 

and add

public class Service1: IService1
{
     public string SayHello(string value) { return string.Format("Hello from {0}", value); }
}

The hosting example in the answer to this question is all code based and as simple as it gets. Use it for a reference.