【问题标题】:WCF & Azure: The contract name could not be found in the list of contracts implementedWCF 和 Azure:在已实施的合同列表中找不到合同名称
【发布时间】:2011-12-01 23:59:28
【问题描述】:

我正在学习如何使用 WCF (Windows Communication Foundation) 进行辅助角色通信。

我完全按照此链接中显示的演示:Worker Role Communication

但是当我执行程序时,我得到了一个 InvalidOperation 异常:

在中找不到合同名称“WcfServiceLibrary1.IService1” 服务实施的合同清单 'System.ServiceModel.ServiceHost'。

这是我的堆栈跟踪:

未处理的异常:System.InvalidOperationException:合同 在列表中找不到名称“WcfServiceLibrary1.IService1” 服务执行的合同 'System.ServiceModel.ServiceHost'。在 System.ServiceModel.ServiceHost.ValidateContractType(类型 实施合同,反射和行为合同集合 反射和行为合同)在 System.ServiceModel.ServiceHost.AddServiceEndpoint(类型 实现Contract,Binding绑定,Uri地址,Uri listenUri)
在 System.ServiceModel.ServiceHost.AddServiceEndpoint(类型 实现的Contract,Binding绑定,Uri地址)在 System.ServiceModel.ServiceHost.AddServiceEndpoint(类型 已实现Contract、Binding 绑定、String 地址、Uri listenUri)
在 System.ServiceModel.ServiceHost.AddServiceEndpoint(类型 实施合同,绑定绑定,字符串地址)在 WorkerRole1.WorkerRole.StartService1() 在 C:\Users\veda\documents\visual studio 2010\Projects\roleToRoleCommInternalEndPt\WorkerRole1\WorkerRole.cs:line 53 在 WorkerRole1.WorkerRole.Run() 中 C:\Users\veda\documents\visual studio 2010\Projects\roleToRoleCommInternalEndPt\WorkerRole1\WorkerRole.cs:line 26 在 Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.StartRoleInternal() 在 Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.StartRole()
在 Microsoft.WindowsAzure.ServiceRuntime.Implementation.Loader.RoleRuntimeBridge.b__1() 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态)在 System.Threading.ThreadHelper.ThreadStart()

worker 角色内部的代码:

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);
        }                               
    }

WCF 代码:

    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);


    }
}

谁能告诉我如何解决这个问题。

【问题讨论】:

    标签: wcf azure


    【解决方案1】:

    您的第一个问题是您告诉 ServiceHost 托管 ServiceHost,而不是您的服务类。这行没有任何意义:

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

    您需要将其替换为:

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

    并添加

    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 完全基于代码,并且非常简单。用作参考。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-01-05
      • 2020-05-26
      • 1970-01-01
      • 1970-01-01
      • 2012-02-17
      • 2014-11-09
      • 2010-10-16
      • 1970-01-01
      相关资源
      最近更新 更多