Cannot call WCF WebService from DLL

2020-04-14 07:40发布

问题:

I'm trying to call a WCF Webservice, from a dll I have made, running inside our CAD Software. I cannot get it to work though.

When I try to establish my proxy, I get the following error:

Could not find endpoint element with name 'BasicHttpBinding_IAxaptaService' and contract 'AxaptaProxy.IAxaptaService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this name could be found in the client element.

I have searched around abit, and I assume the problem is due to my DLL running inside another program. There was some articles about copying EndPoint configuration from the app, to the service, but I didn't quite catch, what I was supposed to do.

Anyone have an idea, as to how I can make this work?

The App.Config, created by my client is this:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IAxaptaService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="None">
                        <transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:4726/LM/AxaptaService.svc"
                binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAxaptaService"
                contract="AxaptaProxy.IAxaptaService" name="BasicHttpBinding_IAxaptaService" />
        </client>
    </system.serviceModel>
</configuration>

I have tried to merge this into my web.config, on the site that hosts the web-service, as this:

  <system.serviceModel>
    <bindings>
      <customBinding>
        <binding name="GetStream.customBinding0">
          <binaryMessageEncoding/>
          <httpTransport/>
        </binding>
      </customBinding>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_IAxaptaService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
              maxBytesPerRead="4096" maxNameTableCharCount="16384" />
          <security mode="None">
            <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
            <message clientCredentialType="UserName" algorithmSuite="Default" />
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <endpointBehaviors>
        <behavior name="AutoCompletionAspNetAjaxBehavior">
          <enableWebScript/>
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="">
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
    <services>
      <service name="AutoCompletion">
        <endpoint address="" behaviorConfiguration="AutoCompletionAspNetAjaxBehavior" binding="webHttpBinding" contract="AutoCompletion"/>
      </service>
      <service name="GetStream">
        <endpoint address="" binding="customBinding" bindingConfiguration="GetStream.customBinding0" contract="GetStream"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
      </service>
    </services>
    <client>
      <endpoint address="http://localhost:4726/LM/AxaptaService.svc"
          binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IAxaptaService"
          contract="AxaptaProxy.IAxaptaService" name="BasicHttpBinding_IAxaptaService" />
    </client>
  </system.serviceModel>

There are a couple of other stuff in there already. I can remove them, if that makes it easier. I've left them in, incase they have some influence on it.

So I tested the service, from a stand-alone winform application, and it works fine. Could it be because of the App.config? Does my config get loaded, for the .dll?

回答1:

You'll need to copy the connection information from MyDll.dll.config to Web.config.

Be careful to merge configuration sections rather that simply adding the new data side-by-side, or replacing it. If there are already sections with the same name, you will probably have to combine them.

Here's an article describing the guts of the WCF portions of app.config:

http://msdn.microsoft.com/en-us/library/ms734663.aspx

The main pieces are:

<system.serviceModel>
   <bindings>
     <!-- various bindings go here... -->
   </bindings>
   <client>
     <!-- endpoints go here... -->
   </client>
</system.serviceModel>

You'll need to combine everything within those nodes - add the various types of endpoint elements and the binding elements to your service's web.config.

So, if you have a config that looks like this:

<system.serviceModel>
   <bindings>
     <someBindingType name="someBinding" />
   </bindings>
   <client>
     <endPoint name="someEndpoint />
   </client>
</system.serviceModel>

You'll need to copy over the someBindingType and endPoint elements. The whole element, including ending tags (if there are any), and child elements.

Make sure you don't duplicate system.serviceModel, bindings or client elements. If they're already there, merge into them rather than creating new elements/duplicating.



回答2:

I finally got it to work!

The problem was, that the app.config does not get loaded, in my .dll project. To fix this, I created the binding, in code, instead of through the app.config, as mentioned in this thread: WCF Configuration without a config file

Thank you for all the help though. Merlyn, without your help, I wouldn't even have gotten this far.