简体   繁体   中英

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

How do I specify the contract in C# code? I want to remove dependency on config file, and getting error:

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>

I'm trying for the first time to use ChannelFactory to specify parms that are normally buried in the config file:

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

What I'm doing is copying my .exe to a new directory, and total removing the <system.serviceModel> element/group. 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#) . 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.

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

  //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).

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

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

Eg you don't have a "bindingConfiguration" setting on the "Binding" class - you need to set everything that's in that bindingConfiguration explicitly.

Can you show us that section, too? (the <bindingConfiguration> you're referencing)

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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