简体   繁体   English

WCF-如何在C#代码中指定端点的合同?

[英]WCF - How to specify endpoint's contract in C# code?

How do I specify the contract in C# code? 如何在C#代码中指定合同? I want to remove dependency on config file, and getting error: 我想删除对配置文件的依赖,并得到错误:

Could not find default endpoint element that references contract 'TFBIC.RCT.HIP. 找不到引用合同'TFBIC.RCT.HIP的默认端点元素。 Components.RCTBizTalk.WcfService_TFBIC_RCT_BizTalk_Orchestrations' in the Servic eModel client configuration section. Servic eModel客户端配置部分中的Components.RCTBizTalk.WcfService_TFBIC_RCT_BizTalk_Orchestrations”。 This might be because no configuration file was found for your application, or because no endpoint element matching this co ntract could be found in the client element. 这可能是因为找不到您的应用程序的配置文件,或者是因为在客户端元素中找不到与该合同匹配的端点元素。

<endpoint 
   address="http://nxwtest08bt1/TFBIC.RCT.BizTalk.Orchestrations/WcfService_TFBIC_RCT_BizTalk_Orchestrations.svc"
   binding="wsHttpBinding" 
   bindingConfiguration="WSHttpBinding_ITwoWayAsync"
   contract="TFBIC.RCT.HIP.Components.RCTBizTalk.WcfService_TFBIC_RCT_BizTalk_Orchestrations"
   name="WSHttpBinding_ITwoWayAsync">
   <identity>
      <userPrincipalName value="NXWTest08BT1\BTAdmin" />
   </identity>
</endpoint>

I'm trying for the first time to use ChannelFactory to specify parms that are normally buried in the config file: 我是第一次尝试使用ChannelFactory指定通常隐藏在配置文件中的parm:

WSHttpBinding myBinding = new WSHttpBinding();

string webServiceURL = "http://localhost/TFBIC.RCT.BizTalk.Orchestrations/WcfService_TFBIC_RCT_BizTalk_Orchestrations.svc";
EndpointAddress myEndpoint = new EndpointAddress(webServiceURL);

ChannelFactory<WcfService_TFBIC_RCT_BizTalk_Orchestrations> myChannelFactory =
     new ChannelFactory<WcfService_TFBIC_RCT_BizTalk_Orchestrations>
         (myBinding, myEndpoint);

// Create a channel and call the web-service via the channel 
WcfService_TFBIC_RCT_BizTalk_Orchestrations wcfClient2 = 
                                       myChannelFactory.CreateChannel();
req.PolicyAction = polAction; 
resp = wcfClient2.WCFSubmitPolicyAction(req);
propResult = resp.PropertyValuation;

I was using Intellisense with the myEndPoint variable, and couldn't find anything like "contract" or even "bindingConfiguration". 我将Intellisense与myEndPoint变量一起使用,找不到“合同”甚至“ bindingConfiguration”之类的东西。

What I'm doing is copying my .exe to a new directory, and total removing the <system.serviceModel> element/group. 我正在做的是将.exe复制到新目录,并完全删除<system.serviceModel>元素/组。 I'd like to try to run entirely without the config file. 我想尝试在没有配置文件的情况下完全运行。 See my related question: NUnit tests that call .DLLs that call WCF Web Services (.NET C#) . 请参阅我的相关问题: 调用.DLL的NUnit测试,该DLL调用WCF Web服务(.NET C#) I'm trying to follow Gilham's answer, even though I didn't fully understand it. 即使我不完全了解,我仍在尝试遵循吉勒姆的答案。 I figured learning how ChannelFactory works was the first step. 我认为了解ChannelFactory的工作原理是第一步。

Thanks, 谢谢,

Neal Walters 尼尔·沃尔特斯

Additional Config File Section: 其他配置文件部分:

<wsHttpBinding>
   <binding 
      name="WSHttpBinding_ITwoWayAsync" closeTimeout="00:01:00"
      openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
      bypassProxyOnLocal="false" transactionFlow="false" 
      hostNameComparisonMode="StrongWildcard" 
      maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
      messageEncoding="Text" textEncoding="utf-8" useDefaultWebProxy="true"
      allowCookies="false">
      <readerQuotas maxDepth="32" maxStringContentLength="8192" 
                    maxArrayLength="16384" maxBytesPerRead="4096" 
                    maxNameTableCharCount="16384" />
      <reliableSession ordered="true" inactivityTimeout="00:10:00"
                       enabled="false" />;
      <security mode="Message">
         <transport clientCredentialType="Windows" proxyCredentialType="None"
                            realm="" />;
         <message clientCredentialType="Windows" negotiateServiceCredential="true"
                  algorithmSuite="Default" establishSecurityContext="true" />
      </security>
   </binding>


Edits Nov-11, 2009 after 5pm Central Time 美国中部时间下午5点后于2009年11月11日编辑
The saga continues, based on what @Marc_s said below, I think I finally figured out that putting the contract into the when defining the channel factory. 根据下面@Marc_s的说法,这个传奇继续存在,我想我终于明白,在定义通道工厂时将合同放入。 Earlier, I was looking for something like endpoint.contract="xxx", since in the Config file, the contract seemed to be sub-parameter of the endpoint. 早些时候,我在寻找诸如endpoint.contract =“ xxx”之类的东西,因为在Config文件中,合同似乎是终结点的子参数。

  //Not-Fully Qualified Contract //ChannelFactory<WcfService_TFBIC_RCT_BizTalk_Orchestrations> myChannelFactory = // new ChannelFactory<WcfService_TFBIC_RCT_BizTalk_Orchestrations>(myBinding, myEndpoint); //Fully Qualified Contract ChannelFactory<TFBIC.RCT.HIP.Components.RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations> myChannelFactory = new ChannelFactory<TFBIC.RCT.HIP.Components.RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations>(myBinding, myEndpoint); 

To get the above to compile, I also has to make a reference to my TBFIC.RCT.HIP.Components (my .DLL Class-Library that calls the WCF service). 为了使以上内容能够编译,我还必须引用我的TBFIC.RCT.HIP.Components(我的.DLL类库,它调用WCF服务)。

So I tried the code above, it runs fine when I have the config file, but still, when I remove the config I get this error: 所以我尝试了上面的代码,当我有配置文件时,它运行良好,但是,当我删除配置时,仍然出现此错误:

Could not find default endpoint element that references contract 'TFBIC.RCT.HIP. 找不到引用合同'TFBIC.RCT.HIP的默认端点元素。 Components.RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations' in the Ser viceModel client configuration section. 服务模型客户端配置部分中的Components.RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations”。 This might be because no configuration f ile was found for your application, or because no endpoint element matching this contract could be found in the client element. 这可能是因为没有为您的应用程序找到配置文件,或者是因为在客户端元素中找不到与该协定匹配的端点元素。

Now, I'm still at a loss what I'm doing wrong to remove the dependencies of the config file. 现在,我仍然茫然无措,无法删除配置文件的依赖项。 I am now using the exact contract it complains as being missing in the channelFactory definition. 我现在正在使用它抱怨在channelFactory定义中缺少的确切合同。 Thanks again! 再次感谢!

Well, you don't have the exact same structure when you create your endpoint in code, than when you do it in config. 嗯,在代码中创建端点的方式与在config中创建端点的方式完全不同。

Eg you don't have a "bindingConfiguration" setting on the "Binding" class - you need to set everything that's in that bindingConfiguration explicitly. 例如,您在“ Binding”类上没有“ bindingConfiguration”设置-您需要显式设置该bindingConfiguration中的所有内容。

Can you show us that section, too? 您也可以向我们展示该部分吗? (the <bindingConfiguration> you're referencing) (您引用的<bindingConfiguration>

The contract is defined when you create the channel factory - I believe that should be okay, as far as I can tell. 合同是在创建渠道工厂时定义的-就我所知,我认为应该没问题。

The error you're getting seems to indicate some parts of your code still try to create a client proxy class (that was most likely created using "Add Service Reference" or svcutil.exe on the command line) and that code tries to read the config from the config file. 您收到的错误似乎表明您代码的某些部分仍在尝试创建客户端代理类(最有可能是在命令行上使用“添加服务引用”或svcutil.exe创建的),并且该代码尝试读取从配置文件配置。

Marc

PS: I think you should be fine now - you create the wsHttpBinding and it uses all the defaults (as in the config), and you define your endpoint address to point to the server where your service is hosted, and in the channel factory you specify the contract being used - that's all there is. PS:我认为您现在应该没问题-创建wsHttpBinding并使用所有默认值(如在配置中),然后定义终结点地址以指向托管服务的服务器,并在通道工厂中指定所使用的合同-仅此而已。 The error points to another "rogue" client proxy being created that still tries to read from the config file - did you add your reference by using "Add Service Reference"? 错误指向正在创建的另一个“流氓”客户端代理,该代理仍尝试从配置文件中读取-您是否通过使用“添加服务引用”添加了引用? If so, please remove that Service Reference from your project. 如果是这样,请从您的项目中删除该服务参考。

I don't know if that's possible at all, but do you really plan on hardcoding values like the URL of the endpoint, or stuff like security credentials and etc? 我完全不知道这是否可行,但是您是否真的打算对诸如端点的URL之类的硬编码值或诸如安全凭证之类的东西进行硬编码? That looks like a really bad idea to me. 对我来说,这似乎是个坏主意。

What we do here is using a ChannelFactory to generate a proxy at runtime based on the values we keep on the configuration file. 我们在这里执行的操作是使用ChannelFactory在运行时根据我们保存在配置文件中的值生成代理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM