创建WCF Web服务实例时发生InvalidOperationException
我有一个从类库引用的WCF Web服务。运行项目后,从类库内部创建服务客户端对象时,我收到带有消息的InvalidOperationException:
Could not find default endpoint element that references contract 'MyServiceReference.IMyService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
我用来创建实例的代码是:
myServiceClient = new MyServiceClient();
MyServiceClient继承自的位置
System.ServiceModel.ClientBase
我该如何解决?
注意:我有一个单独的控制台应用程序,它可以简单地创建相同的服务对象并对其进行调用,并且可以正常工作。
解决方案
回答
如果我们发布了app.config文件,这可能会有所帮助,因为这种错误倾向于指出<endpoint>
块中的问题。确保合同属性对我们而言正确。
编辑:尝试完全限定合同价值;使用完整的名称空间。我认为这是必要的。
回答
Here is my app.config file of the class library:
我们应该将此配置设置放入主应用程序的配置文件中。 .NET应用程序(正在调用类库)使用其自身配置文件中的数据,而不是库配置文件中的数据。
回答
我有一个类似的案例。我有一个称为Web服务的类库,然后有一个称为类库的.DLL的.EXE。我认为使用的是.EXE的配置文件,而不是.DLL的配置文件。
但是正如Richard上面所说,我必须完全限定名称空间。有点痛苦。
以下正是我所做的更改。痛苦的是我不得不在两个地方进行更改,
一个在创建服务引用时生成的reference.cs中,另一个在配置文件中。因此,每次更改Web服务并执行"更新参考"时,都必须再次对Ccode进行更改。
1)我们必须按如下所示实际更改reference.cs中的ConfigurationName:
来自:`[System.ServiceModel.ServiceContractAttribute(Namespace =" http://TFBIC.RCT.BizTalk.Orchestrations",ConfigurationName =" RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations"])
到:`[System.ServiceModel.ServiceContractAttribute(Namespace =" http://TFBIC.RCT.BizTalk.Orchestrations",ConfigurationName =" TFBIC.RCT.HIP.Components.RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations"]]
2),然后还如下更改所有相关app.config(对于.dlls和.exes)中的合同值:
从:
<端点地址= http://nxwtest08bt1.dev.txfb-ins.com/TFBIC.RCT.BizTalk.Orchestrations/WcfService_TFBIC_RCT_BizTalk_Orchestrations.svc
binding =" wsHttpBinding" bindingConfiguration =" WSHttpBinding_ITwoWayAsync"
contract =" RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations"
name =" WSHttpBinding_ITwoWayAsync">
到:
<端点地址= http://nxwtest08bt1.dev.txfb-ins.com/TFBIC.RCT.BizTalk.Orchestrations/WcfService_TFBIC_RCT_BizTalk_Orchestrations.svc
binding =" wsHttpBinding" bindingConfiguration =" WSHttpBinding_ITwoWayAsync"
contract =" TFBIC.RCT.HIP.Components.RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations" name =" WSHttpBinding_ITwoWayAsync">
为了清楚起见,我怎么知道完整的名称空间是什么?
该程序的名称空间是TFBIC.RCT.HIP。在其中,Ccode还有一个添加功能
命名空间声明:
namespace RCTHipComponents
回答
或者,我们可以在代码中设置端点:
http://msdn.microsoft.com/en-us/library/ms731862.aspx
BasicHttpBinding binding = new BasicHttpBinding(); EndpointAddress address = new EndpointAddress("http://url-to-service/"); // Create a client that is configured with this address and binding. MyServiceClient client = new MyServiceClient(binding, address);