c# - WCF - 如何在 C# 代码中指定端点的契约(Contract)?

标签 c# .net wcf

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

Could not find default endpoint element that references contract 'TFBIC.RCT.HIP. Components.RCTBizTalk.WcfService_TFBIC_RCT_BizTalk_Orchestrations' in the Servic eModel client configuration section. 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>

我第一次尝试使用 ChannelFactory 来指定通常隐藏在配置文件中的参数:

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 变量时,找不到任何类似“contract”甚至“bindingConfiguration”的东西。

我正在做的是将我的 .exe 复制到一个新目录,并完全删除 <system.serviceModel>元素/组。我想尝试在没有配置文件的情况下完全运行。请参阅我的相关问题:NUnit tests that call .DLLs that call WCF Web Services (.NET C#) .我试图遵循 Gilham 的回答,尽管我并不完全理解它。我认为了解 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>


2009 年 11 月 11 日中部时间下午 5 点后编辑
传奇还在继续,根据@Marc_s 在下面所说的内容,我想我终于弄清楚了在定义 channel 工厂时将契约(Contract)放入。早些时候,我一直在寻找类似 endpoint.contract="xxx"的内容,因为在配置文件中,契约(Contract)似乎是端点的子参数。

        //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(我调用 WCF 服务的 .DLL 类库)。

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

Could not find default endpoint element that references contract 'TFBIC.RCT.HIP. Components.RCTWebService.WcfService_TFBIC_RCT_BizTalk_Orchestrations' in the Ser viceModel client configuration section. 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.

现在,我仍然不知道我做错了什么来删除配置文件的依赖项。我现在正在使用它提示在 channelFactory 定义中丢失的确切契约(Contract)。再次感谢!

最佳答案

嗯,在代码中创建端点与在配置中创建端点时的结构并不完全相同。

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

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

契约(Contract)是在您创建 channel 工厂时定义的 - 据我所知,我认为这应该没问题。

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

马克

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

关于c# - WCF - 如何在 C# 代码中指定端点的契约(Contract)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1716251/

相关文章:

.net - 在 .NET 中模拟 VBA 算术

c# - 用于从任何类型的列表创建 html 表的通用 HtmlHelper

c# - 找不到 Restful Web 服务端点

c# - 将流转换为文件

c# - 与 System.Web.Mvc 的版本冲突

c# - 是否可以检索Word中所有可用的图表模板

c# - 定义 Python 类

c# - 如何识别现有Visual Studio项目的项目类型?

c# - MSBuild error opening vs2017 csproj files (The tools version "15.0"is unrecognized...)

wcf - 在基于 WCF 的企业应用程序中使用 Entity Framework 的哪个变体