문제

자체 계약을 맺은 여러 WCF 서비스가 포함 된 어셈블리가 있습니다. 모두 잘 작동합니다. 서비스의 app.config의 서비스 구성은 다음과 같습니다.

<services>
  <service behaviorConfiguration="WcfService.AlyzaServiceBehavior"
    name="Sam.Alyza.WcfService.ServiceWebsites">
    <endpoint address="" binding="netTcpBinding" contract="Sam.Alyza.WcfInterface.IServiceWebsites">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8731/Design_Time_Addresses/SamAlyza/Websites/" />
      </baseAddresses>
    </host>
  </service>
  <service behaviorConfiguration="Sam.Alyza.WcfService.LogReaderServiceBehavior"
    name="Sam.Alyza.WcfService.ServiceLogReader">
    <endpoint address="" binding="netTcpBinding" contract="Sam.Alyza.WcfInterface.IServiceLogReader">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8731/Design_Time_Addresses/SamAlyza/LogReader/" />
      </baseAddresses>
    </host>
  </service>
  <service behaviorConfiguration="Sam.Alyza.WcfService.ServiceSystemverwaltungBehavior"
    name="Sam.Alyza.WcfService.ServiceSystemverwaltung">
    <endpoint address="" binding="netTcpBinding" contract="Sam.Alyza.WcfInterface.IServiceSystemverwaltung">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8731/Design_Time_Addresses/SamAlyza/Systemverwaltung/" />
      </baseAddresses>
    </host>
  </service>
  [...]
</services>

더 많은 계약을 통해 더 큰 프로젝트를 염두에두고 있기 때문에 다른 서비스 계약간에 Baseaddress를 공유 할 수있는 방법을 원합니다.
이것이 다른 계약과 엔드 포인트를 가진 하나의 서비스 일 뿐이라면 Omon Baseaddress를 설정할 수 있지만 하나 이상의 서비스를 위해 공통 BaseDdress를 어떻게 설정합니까?

물론 고객과 비슷한 것이 필요합니다.

도움이 되었습니까?

해결책

한 클래스의 모든 계약을 결합 할 수 있으므로 계약 당 1 개의 서비스와 하나 이상의 엔드 포인트가있는 서비스가 있습니다.

하나의 큰 클래스 파일을 피하려면 부분 키워드 (C#을 사용하는 Asuming)를 사용하여 여러 파일에 클래스를 분할 할 수 있습니다. 각 파일은 하나의 계약을 구현할 수 있으므로 개별 인터페이스를 훨씬 쉽게 유지할 수 있습니다.

C ++에서는 #includes 또는 다중 상속을 사용할 수 있지만 많은 징계를 의미합니다 ...

구성은 다음과 같습니다.

<services>
  <service behaviorConfiguration="WcfService.AlyzaServiceBehavior"
    name="Sam.Alyza.WcfService.ServiceAll">
    <endpoint address="Websites/" binding="netTcpBinding" contract="Sam.Alyza.WcfInterface.IServiceWebsites">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="LogReader/" binding="netTcpBinding" contract="Sam.Alyza.WcfInterface.IServiceLogReader">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="Systemverwaltung/" binding="netTcpBinding" contract="Sam.Alyza.WcfInterface.IServiceSystemverwaltung">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange" />
    <host>
      <baseAddresses>
        <add baseAddress="net.tcp://localhost:8731/Design_Time_Addresses/SamAlyza/" />
      </baseAddresses>
    </host>
  </service>
</services>

다른 팁

서비스는 BaseadDress 값 (NET.TCP 포트 공유 서비스가 실행중인 경우 포트 #을 포함하여)을 공유 할 수 있습니다. 독특 해야하는 엔드 포인트 주소입니다. 구성 파일에서 각 ServiceHost의 MEX 엔드 포인트에는 "MEX"주소가 있음을 알 수 있습니다. 다른 엔드 포인트에는 빈 문자열 주소가 있습니다. WCF에 대한 엔드 포인트에 대한 상대 주소를 제공하면 (적어도 구성 파일에서) 기본 주소가 선정됩니다. 따라서 Logreader 서비스의 MEX 엔드 포인트 주소는 "net.tcp : // localhost : 8731/design_time_addresses/samalyza/logreader/mex"입니다.

메인 서비스 엔드 포인트에 상대 주소가 설정되지 않았으므로 ServiceHost의 기본 주소는 기본 서비스 엔드 포인트의 실제 주소로 사용됩니다. 두 개의 엔드 포인트가 uri.absolutepath 값을 겹치는 것은 없기 때문에 예제로 기본 주소 값을 공유 할 수 없다고 믿게됩니다. WCF 서비스를 호스팅하는 ServiceHost 클래스에는 엔드 포인트가 내장되어 있지 않지만 ServiceEndpoint 클래스에는 제공하는 설정에 따라 채워질 수있는 ListenUri 속성이 있습니다.

엔드 포인트 요소에 고유 한 상대 주소 값을 설정하는 한 예제의 Baseaddress 값을 모든 일치로 변경하면 모든 것이 작동해야합니다. 그러나 MEX 엔드 포인트로 일부 도전에 도전 할 수 있습니다. 모두 현재 주소 "MEX"가 있기 때문입니다. 독특하게 만들면 괜찮을 것입니다.

이제, 나는 당신이 단순히 이러한 서비스가 기본 주소가 아닌 네임 스페이스를 공유하기를 원하지 않다고 확신합니까?

사용자 정의 ServiceHostFactory를 사용하는 경우 기본 주소를 코드로 설정할 수도 있습니다.

구성에서 일부 앱 설정을 가질 수 있습니다.

<configuration>
  <appSettings>
    <add key="BaseAddress" value="http://localhost:1234" />
  </appSettings>
<configuration>

그런 다음 사용자 정의 ServiceHostFactory를 만드십시오.

public sealed class MyServiceHostFactory : ServiceHostFactory
{
    protected override ServiceHost CreateServiceHost(Type serviceType, Uri[] baseAddresses)
    {
        var baseAddy = ConfigurationManager.AppSettings["BaseAddress"];
        baseAddresses.Add(new Uri(baseAddy));
        return new ServiceHost(serviceType, baseAddresses);
    }

}

그런 다음 해당 공장을 사용하려면 .SVC 파일을 변경해야합니다.

<%@ ServiceHost Language="C#" Debug="true" Service="MyApp.MyService" CodeBehind="MyService.svc.cs" Factory="MyApp.MyServiceHostFactory" %>
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top