Use WCF without app.config

I need to use the WCF service from a DLL, so I don't have a configuration file to read binding configurations.

It is very difficult for me to make it work. In the end, as a very simple solution, I add a link to WCF and create it as follows:

        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 just works. When I try to use it on another computer, I get this error:

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

The IIS host is set to Anonymous, so I think it should just work.

Any help?

EDIT: Service configuration 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>
+3
source share
2 answers

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();

Dns , . ( Windows), defult Windows.

+4

HTTP, .

+2

Source: https://habr.com/ru/post/1796251/


All Articles