开发者

Consume WCF without app.config

开发者 https://www.devze.com 2023-02-14 15:11 出处:网络
I need to consume a WCF service from a DLL, hence i don\'t have any configuration file to read bindings configurations from.

I need to consume a WCF service from a DLL, hence i don't have any configuration file to read bindings configurations from.

I'm having a really hard time getting it to work. In the end, as a very simple solution, I add a reference to the WCF and instantiate it like this:

        WSHttpBinding binding = new WSHttpBinding();
        EndpointAddress address = new EndpointAddress("http://myhost.net/Service.svc");

        ServiceReference.ServiceClient client = new ServiceReference.ServiceClient(binding, address);
        var result = client.Method1();

In localhost this simply works. When trying it from another machine, i get this error:

The request for security token could not be satisfied because authentication failed.

In the host, IIS is set to "Anonymous", so I guess it should simply work.

Any help?

EDIT: Service Config File

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true"/>
  </system.web>
  <system.serviceModel>
    <services>
      <service name="Mai.MyPlanner.Service">
        <开发者_运维问答endpoint address="" binding="wsHttpBinding" contract="Mai.MyPlanner.IService">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://MY_SERVICE"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>

          <serviceMetadata httpGetEnabled="True"/>
          <serviceDebug includeExceptionDetailInFaults="False"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

<connectionStrings>
  <!-- PROD -->

  <!-- TEST -->
</connectionStrings>

<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/></startup></configuration>


Use this code:

WSHttpBinding binding = new WSHttpBinding();
EndpointIdentity identity = EndpointIdentity.CreateDnsIdentity("localhost");
EndpointAddress address = new EndpointAddress("http://myhost.net/Service.svc", identity);

ServiceReference.ServiceClient client = new ServiceReference.ServiceClient(binding, address);
var result = client.Method1();

You will still need to pass Dns identity value and address into your method calling this code. Also this type of configuration will work only in intranet (same windows domain) because it by defult uses message security and Windows authentication.


Use basic http binding instead, if you do not require security.


What if you'd have a ServiceHost that would take a ServiceEndpoint such as below:

// Step 3 Add a service endpoint. selfHost.AddServiceEndpoint(typeof(ICalculator), binding, "CalculatorService");

How would you still programmatically specify App.config elements such as:

<services>
  <service name="GettingStartedLib.CalculatorService">
    <host>
      <baseAddresses>
        <add baseAddress = "http://localhost:8000/GettingStarted/CalculatorService" />
      </baseAddresses>
    </host>
    <!-- Service Endpoints -->
    <!-- Unless fully qualified, address is relative to base address supplied above -->
    <endpoint address="" binding="wsHttpBinding" contract="GettingStartedLib.ICalculator">
      <!-- 
          Upon deployment, the following identity element should be removed or replaced to reflect the 
          identity under which the deployed service runs.  If removed, WCF will infer an appropriate identity 
          automatically.
      -->
      <identity>
        <dns value="localhost"/>
      </identity>
    </endpoint>
    <!-- Metadata Endpoints -->
    <!-- The Metadata Exchange endpoint is used by the service to describe itself to clients. --> 
    <!-- This endpoint does not use a secure binding and should be secured or removed before deployment -->
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
  </service>
</services>
0

精彩评论

暂无评论...
验证码 换一张
取 消