繁体   English   中英

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

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

如何在C#代码中指定合同? 我想删除对配置文件的依赖,并得到错误:

找不到引用合同'TFBIC.RCT.HIP的默认端点元素。 Servic eModel客户端配置部分中的Components.RCTBizTalk.WcfService_TFBIC_RCT_BizTalk_Orchestrations”。 这可能是因为找不到您的应用程序的配置文件,或者是因为在客户端元素中找不到与该合同匹配的端点元素。

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

我是第一次尝试使用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;

我将Intellisense与myEndPoint变量一起使用,找不到“合同”甚至“ bindingConfiguration”之类的东西。

我正在做的是将.exe复制到新目录,并完全删除<system.serviceModel>元素/组。 我想尝试在没有配置文件的情况下完全运行。 请参阅我的相关问题: 调用.DLL的NUnit测试,该DLL调用WCF Web服务(.NET C#) 即使我不完全了解,我仍在尝试遵循吉勒姆的答案。 我认为了解ChannelFactory的工作原理是第一步。

谢谢,

尼尔·沃尔特斯

其他配置文件部分:

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


美国中部时间下午5点后于2009年11月11日编辑
根据下面@Marc_s的说法,这个传奇继续存在,我想我终于明白,在定义通道工厂时将合同放入。 早些时候,我在寻找诸如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); 

为了使以上内容能够编译,我还必须引用我的TBFIC.RCT.HIP.Components(我的.DLL类库,它调用WCF服务)。

所以我尝试了上面的代码,当我有配置文件时,它运行良好,但是,当我删除配置时,仍然出现此错误:

找不到引用合同'TFBIC.RCT.HIP的默认端点元素。 服务模型客户端配置部分中的Components.RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations”。 这可能是因为没有为您的应用程序找到配置文件,或者是因为在客户端元素中找不到与该协定匹配的端点元素。

现在,我仍然茫然无措,无法删除配置文件的依赖项。 我现在正在使用它抱怨在channelFactory定义中缺少的确切合同。 再次感谢!

嗯,在代码中创建端点的方式与在config中创建端点的方式完全不同。

例如,您在“ Binding”类上没有“ bindingConfiguration”设置-您需要显式设置该bindingConfiguration中的所有内容。

您也可以向我们展示该部分吗? (您引用的<bindingConfiguration>

合同是在创建渠道工厂时定义的-就我所知,我认为应该没问题。

您收到的错误似乎表明您代码的某些部分仍在尝试创建客户端代理类(最有可能是在命令行上使用“添加服务引用”或svcutil.exe创建的),并且该代码尝试读取从配置文件配置。

PS:我认为您现在应该没问题-创建wsHttpBinding并使用所有默认值(如在配置中),然后定义终结点地址以指向托管服务的服务器,并在通道工厂中指定所使用的合同-仅此而已。 错误指向正在创建的另一个“流氓”客户端代理,该代理仍尝试从配置文件中读取-您是否通过使用“添加服务引用”添加了引用? 如果是这样,请从您的项目中删除该服务参考。

我完全不知道这是否可行,但是您是否真的打算对诸如端点的URL之类的硬编码值或诸如安全凭证之类的东西进行硬编码? 对我来说,这似乎是个坏主意。

我们在这里执行的操作是使用ChannelFactory在运行时根据我们保存在配置文件中的值生成代理。

暂无
暂无

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

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