我正在学习如何使用WCF(Windows Communication Foundation)进行工作者角色通信。
我完全按照此链接显示的演示:Worker Role Communication
但是当我执行该程序时,我收到了InvalidOperation异常:
找不到合约名称'WcfServiceLibrary1.IService1' 服务实施的合同清单 'System.ServiceModel.ServiceHost'。
这是我的Stack Trace:
未处理的异常:System.InvalidOperationException:合同 在列表中找不到名称“WcfServiceLibrary1.IService1” 服务实施的合同 'System.ServiceModel.ServiceHost'。在 System.ServiceModel.ServiceHost.ValidateContractType(类型 implementedContract,ReflectedAndBehaviorContractCollection reflectAndBehaviorContracts)at System.ServiceModel.ServiceHost.AddServiceEndpoint(类型 implementsContract,绑定绑定,Uri地址,Uri listenUri)
在System.ServiceModel.ServiceHost.AddServiceEndpoint(Type implementsContract,Binding binding,Uri address)at System.ServiceModel.ServiceHost.AddServiceEndpoint(类型 implementedContract,Binding binding,String address,Uri listenUri)
在System.ServiceModel.ServiceHost.AddServiceEndpoint(Type implementsContract,Binding binding,String address)at WorkerRole1.WorkerRole.StartService1()in C:\ Users \ veda \ documents \ visual studio 2010 \项目\ roleToRoleCommInternalEndPt \ WorkerRole1 \ WorkerRole.cs:行 53在WorkerRole1.WorkerRole.Run()中 C:\ Users \ veda \ documents \ visual studio 2010 \项目\ roleToRoleCommInternalEndPt \ WorkerRole1 \ WorkerRole.cs:行 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)at System.Threading.ExecutionContext.Run(执行上下文 executionContext,ContextCallback回调,对象状态)at System.Threading.ThreadHelper.ThreadStart()
工作者角色中的代码:
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);
}
}
有谁能告诉我如何解决这个问题。
答案 0 :(得分:0)
您的第一个问题是您告诉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是基于代码的,并且尽可能简单。用它作为参考。