I have a WCF Web Service which is referenced from a class library. After the project is run, when creating the service client object from inside a class library, I receive an InvalidOperationException with message:
我有一个从类库引用的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.
无法在ServiceModel客户端配置部分中找到引用合同“MyServiceReference.IMyService”的默认端点元素。这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素。
The code I am using to create the instance is:
我用来创建实例的代码是:
myServiceClient = new MyServiceClient();
where MyServiceClient inherits from
MyServiceClient继承自的地方
System.ServiceModel.ClientBase
How do I solve this?
我该如何解决这个问题?
Note: I have a seperate console application which simply creates the same service object and makes calls to it and it works without no problems.
注意:我有一个单独的控制台应用程序,它只是创建相同的服务对象并调用它,它没有任何问题。
4 个解决方案
#1
5
Here is my app.config file of the class library:
这是我的类库的app.config文件:
You should put this configuration settings to main app's config file. .NET application (which is calling your class library) uses data from it's own config file not from your library config file.
您应该将此配置设置放到主应用程序的配置文件中。 .NET应用程序(调用您的类库)使用来自它自己的配置文件的数据,而不是来自库配置文件。
#2
8
Or you can set the endpoint in your code:
或者您可以在代码中设置端点:
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);
#3
1
I had a similar case. I had a class-library that called a web service, then I had an .EXE that called the class-lib's .DLL. I think it's the .EXE's config file that is used and not that of the .DLL config.
我有类似的情况。我有一个叫做Web服务的类库,然后我有一个调用class-lib的.DLL的.EXE。我认为这是.EXE的配置文件,而不是.DLL配置。
But as Richard said above, I had to fully-qualify the namespace. It's a bit of a pain. Below is exactly what I changed. The pain is that I had to change it in two places, one in the reference.cs that is generated when you create a service reference, and the other in the config file. Thus, everytime I change the web service and do an "Update Reference" I have to make the change to the C# code again.
但正如理查德在上面所说,我必须完全限定名称空间。这有点痛苦。以下是我改变的内容。痛苦在于我必须在两个地方更改它,一个在创建服务引用时生成的reference.cs中,另一个在配置文件中生成。因此,每次我更改Web服务并执行“更新引用”时,我都必须再次更改C#代码。
1) You must actually change the ConfigurationName in the reference.cs as follows:
1)您必须实际更改reference.cs中的ConfigurationName,如下所示:
From: [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 =“RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations”)]
To: [System.ServiceModel.ServiceContractAttribute(Namespace = "http://TFBIC.RCT.BizTalk.Orchestrations", ConfigurationName = "TFBIC.RCT.HIP.Components.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) and then also change the “contract” value in all related app.config (for .dll’s and .exe’s) as follows:
2)然后还更改所有相关app.config中的“合同”值(对于.dll和.exe的),如下所示:
From:
<endpoint address=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">
To:
<endpoint address=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">
Just to be clear - how did I know what the full namespace was? The program's namespace was TFBIC.RCT.HIP. Inside that, the C# code has one additional namespace statement:
只是要清楚 - 我怎么知道完整命名空间是什么?该程序的命名空间是TFBIC.RCT.HIP。在其中,C#代码有一个额外的命名空间声明:
namespace RCTHipComponents
#4
0
It would probably help if you posted your app.config file, since this kind of error tends to point to a problem in the <endpoint>
block. Make sure the contract attribute seems right to you.
如果您发布了app.config文件,它可能会有所帮助,因为这种错误往往指向
Edit: Try fully qualifying your contract value; use the full namespace. I think that is needed.
编辑:尝试完全限定合同价值;使用完整的命名空间。我认为这是必要的。
#1
5
Here is my app.config file of the class library:
这是我的类库的app.config文件:
You should put this configuration settings to main app's config file. .NET application (which is calling your class library) uses data from it's own config file not from your library config file.
您应该将此配置设置放到主应用程序的配置文件中。 .NET应用程序(调用您的类库)使用来自它自己的配置文件的数据,而不是来自库配置文件。
#2
8
Or you can set the endpoint in your code:
或者您可以在代码中设置端点:
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);
#3
1
I had a similar case. I had a class-library that called a web service, then I had an .EXE that called the class-lib's .DLL. I think it's the .EXE's config file that is used and not that of the .DLL config.
我有类似的情况。我有一个叫做Web服务的类库,然后我有一个调用class-lib的.DLL的.EXE。我认为这是.EXE的配置文件,而不是.DLL配置。
But as Richard said above, I had to fully-qualify the namespace. It's a bit of a pain. Below is exactly what I changed. The pain is that I had to change it in two places, one in the reference.cs that is generated when you create a service reference, and the other in the config file. Thus, everytime I change the web service and do an "Update Reference" I have to make the change to the C# code again.
但正如理查德在上面所说,我必须完全限定名称空间。这有点痛苦。以下是我改变的内容。痛苦在于我必须在两个地方更改它,一个在创建服务引用时生成的reference.cs中,另一个在配置文件中生成。因此,每次我更改Web服务并执行“更新引用”时,我都必须再次更改C#代码。
1) You must actually change the ConfigurationName in the reference.cs as follows:
1)您必须实际更改reference.cs中的ConfigurationName,如下所示:
From: [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 =“RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations”)]
To: [System.ServiceModel.ServiceContractAttribute(Namespace = "http://TFBIC.RCT.BizTalk.Orchestrations", ConfigurationName = "TFBIC.RCT.HIP.Components.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) and then also change the “contract” value in all related app.config (for .dll’s and .exe’s) as follows:
2)然后还更改所有相关app.config中的“合同”值(对于.dll和.exe的),如下所示:
From:
<endpoint address=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">
To:
<endpoint address=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">
Just to be clear - how did I know what the full namespace was? The program's namespace was TFBIC.RCT.HIP. Inside that, the C# code has one additional namespace statement:
只是要清楚 - 我怎么知道完整命名空间是什么?该程序的命名空间是TFBIC.RCT.HIP。在其中,C#代码有一个额外的命名空间声明:
namespace RCTHipComponents
#4
0
It would probably help if you posted your app.config file, since this kind of error tends to point to a problem in the <endpoint>
block. Make sure the contract attribute seems right to you.
如果您发布了app.config文件,它可能会有所帮助,因为这种错误往往指向
Edit: Try fully qualifying your contract value; use the full namespace. I think that is needed.
编辑:尝试完全限定合同价值;使用完整的命名空间。我认为这是必要的。