2

我创建了一个 WCF 服务,其中我有一个服务合同和多个服务类实现相同的合同。您能否告诉我在 app.config 中编辑什么以及如何在控制台应用程序中托管此服务。我有一份服务合同,我在 wcf 的三个 *.cs 文件中实施了这份合同。

谢谢。

这是我写的 App.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <system.web>
    <compilation debug="true" />
  </system.web>
  <!-- When deploying the service library project, the content of the config file must be added to the host's 
  app.config file. System.Configuration does not support config files for libraries. -->
  <system.serviceModel>
    <services>
      <service name="ProductService.Service1">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8732/Design_Time_Addresses/ProductService/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="" binding="wsHttpBinding" contract="ProductService.IService1">
          <!-- 
              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>
        <service name="ProductService.RegistrationService">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:7777/Design_Time_Addresses/ProductService/RegistrationService/"/>
                </baseAddresses>
            </host>
            <endpoint address="" binding="wsHttpBinding" contract="ProductService.IService1">
                <identity>
                    <dns value ="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
        <service name="ProductService.ProductService">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8888/Design_Time_Addresses/ProductService/ProductService/"/>
                </baseAddresses>
            </host>
            <endpoint address="" binding="wsHttpBinding" contract="ProductService.IService1">
                <identity>
                    <dns value ="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
        <service name="ProductService.CatogeryService">
            <host>
                <baseAddresses>
                    <add baseAddress="http://localhost:8080/Design_Time_Addresses/ProductService/CatogeryService/"/>
                </baseAddresses>
            </host>
            <endpoint address="" binding="wsHttpBinding" contract="ProductService.IService1">
                <identity>
                    <dns value ="localhost"/>
                </identity>
            </endpoint>
            <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <!-- To avoid disclosing metadata information, 
          set the value below to false and remove the metadata endpoint above before deployment -->
          <serviceMetadata httpGetEnabled="True"/>
          <!-- To receive exception details in faults for debugging purposes, 
          set the value below to true.  Set to false before deployment 
          to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>

</configuration>
4

1 回答 1

0

我想你已经在服务合同上定义了几个服务操作?你试图做的事情是不可能的,也不是可取的。

因为一个逻辑服务只与一个服务合同相关联,所以您不能为同一个合同添加多个服务。

可以为每个服务合同添加多个服务端点,但不能基于每个操作。您可能希望通过两种或多种不同的传输方式(例如 HTTP 和 HTTPS)提供服务。

您可以使用单个服务的所有操作(您定义了 4 个服务,所以只需摆脱最后三个),但我认为这不是您想要做的。

要在单独的服务端点上托管每个服务操作,您需要将服务合同分解为多个合同。

这是更可取的,因为它减少了端点之间的耦合,并且更容易理解正在发生的事情。

于 2012-09-11T09:43:59.890 回答