tags:

views:

718

answers:

2

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: http://stackoverflow.com/questions/1711588/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!

+1  A: 

Well, you don't have the exact same structure when you create your endpoint in code, than when you do it in config.

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

marc_s
I posted what I think you were asking for at bottom of my original post above. Maybe I need a ServiceHost object as shown here: http://msdn.microsoft.com/en-us/library/ms731925.aspx? But I couldn't quite figure out how it was actually used.
NealWalters
@Neal: those are all the default values - so you should be good to go with the wsHttpBinding object you create in your code.
marc_s
@Neal: if you're on the server side (where you service is hosted), then yes - you need a ServiceHost to "host" your service (as the name implies). But I assume this is on the client side, using / calling a service - right? In that case, you're absolutely doing the right thing create a channel factory and a channel from it. That's not the problem.
marc_s
@Marc_s: Ok, IIS is my host - I didn't get that was what that object was about. I had to manually change the contract in my config file and in the reference.cs that was generated (to be fully qualified). So now, I need to specify that same contract name in the client C# code now.
NealWalters
@Marc_s: See http://stackoverflow.com/questions/24993/invalidoperationexception-while-creating-wcf-web-service-instance/1711550#1711550 on how/why I had to change contract. I have a BizTalk orchestration published as WCF service. Not sure how I could remove the service reference, because I need the request/response variables (which were defined in XML in BizTalk) - unless I were to run XSD utility and build C# class from them. I had to put app.config under HIS Server today to be able to call my WCF-based DLL.
NealWalters
P.S. Thanks again by the way
NealWalters
@Marc_s: I updated question again with my latest experiments (beginning where it says "the saga continues").
NealWalters
Hi Neal, ok, so you can't remove the Service Reference; can you check and make sure your default client proxy `(something-or-another)Client` from the generated service reference isn't being instantiated anywhere in your code? If you only need the data portion of the service contract - no problem. But if you delete the config file, you can no longer instantiate the default client proxy in that generated file.
marc_s
A: 

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.

axel_c
Here's my purpose: http://stackoverflow.com/questions/1711588/nunit-tests-that-call-dlls-that-call-wcf-web-services-net-c
NealWalters
Here's another example - I'm running my .DLL under HIS (Host Integration Server) as a service. Just now, to get things to work, I had to add a app.config to the windows service to get it to work.
NealWalters