Asp.net Self Hosted WSF Service WSDL with Relative Paths

I am working on a WCF application that will be deployed to different servers along the way, and I would not forget to change app.config every time I deploy it. At first, my app.config serviceModel section looked like this:

<system.serviceModel>  
<serviceHostingEnvironment aspNetCompatibilityEnabled="false" />  
<behaviors>  
    <serviceBehaviors>  
        <behavior name="MyDefaultServiceBehavior">   
            <serviceMetadata httpGetEnabled="true" httpGetUrl="http://localhost:8888/MyService" />  
            <serviceDebug includeExceptionDetailInFaults="true" />  
        </behavior>  
    </serviceBehaviors>  
</behaviors>  
<services>  
    <service behaviorConfiguration="MyDefaultServiceBehavior" name="MyService">   
        <endpoint address="net.tcp://localhost:9001/MyService" binding="netTcpBinding" contract="IMyService" name="NetTcpBinding_IMyService" />  
    </service>  
</services>  

This works fine during the development process when I accessed a service running on my local machine. When I deployed it, the WSDL contained absolute paths that still pointed to localhost:

<xsd:import schemaLocation=http://localhost:8888/MyService?xsd=xsd0 namespace="http://tempuri.org/" />

So, I can change httpGetUrl in app.config like this:

<serviceMetadata httpGetEnabled="true" httpGetUrl=http://devserver1:8888/MyService />

And now wsdl works correctly on this server. The problem is that I have to manually set the address in every app.config that is deployed.

: 1. wsdl , ?

2. wsdl?

. , , wsdl.

-, , - , , -.

! Daniel

+3
2

, , . - , , - .

app.config httpGetUrl, "myServiceServer":

<serviceMetadata httpGetEnabled="true" httpGetUrl=http://myServiceServer:8888/MyService />

, - , "myServiceServer" IP-. , IP- IP- . , VPN- - NAT-.

+2

httpGetUrl , . WSDL .

WCF (, IIS), ServiceHostFactory, ServiceHost. :

using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Description;

namespace WebApplication
{
  public class TestServiceHostFactory : ServiceHostFactory
  {
     protected override ServiceHost CreateServiceHost(Type serviceType, 
                                                      Uri[] baseAddresses)
      {
        ServiceHost host = base.CreateServiceHost(serviceType, 
                                                  baseAddresses);
        ServiceMetadataBehavior metadataBehavior = 
                                new ServiceMetadataBehavior();
        metadataBehavior.HttpGetEnabled = true;
        metadataBehavior.HttpGetUrl = new Uri(string.Format(
                              "http://{0}/WebApplication/TestService.svc", 
                              Environment.MachineName));
        host.Description.Behaviors.Add(metadataBehavior);
        return host;
      }
  }
}

factory .svc :

<%@ ServiceHost Language="C#" 
                Service="WebApplication.TestService" 
                CodeBehind="TestService.svc.cs" 
                Factory="WebApplication.TestServiceHostFactory" %>

WCF, :

ServiceHost host = new ServiceHost(typeof(WebApplication.TestService));
ServiceMetadataBehavior metadataBehavior = new ServiceMetadataBehavior();
metadataBehavior.HttpGetEnabled = true;
metadataBehavior.HttpGetUrl = new Uri(string.Format(
                              "http://{0}/WebApplication/TestService.svc", 
                              Environment.MachineName));
host.Description.Behaviors.Add(metadataBehavior);
+3

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


All Articles