如何在单个应用程序中托管wcf rest和soap服务

时间:2014-01-11 15:51:30

标签: .net web-services wcf-data-services

我创建了一个启用了SOAP和REST功能的wcf服务。当我使用visual studio启动服务时,服务启动,我可以使用webclient和浏览器使用url来使用它。 但我希望该服务可以通过应用程序文件(控制台或Windows)启动。我必须能够同时使用肥皂和休息功能。 这是我的配置文件:

<?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="WcfServiceLibrary1.Service1" behaviorConfiguration="WcfServiceLibrary1.Service1Behavior">
        <host>
          <baseAddresses>
            <add baseAddress = "http://localhost:8732/Design_Time_Addresses/WcfServiceLibrary1/Service1/" />
          </baseAddresses>
        </host>
        <!-- Service Endpoints -->
        <!-- Unless fully qualified, address is relative to base address supplied above -->
        <endpoint address ="soap" binding="wsHttpBinding" contract="WcfServiceLibrary1.IService1">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="rest" binding="webHttpBinding" contract="WcfServiceLibrary1.IService1" behaviorConfiguration="restEndpointBehavior">
          <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>
    <behaviors>
      <endpointBehaviors>
      <behavior name="restEndpointBehavior">
        <webHttp />
      </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="WcfServiceLibrary1.Service1Behavior">
          <!-- 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>

I tried adding the service reference to the console application that generated a app.config with the following code:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <wsHttpBinding>
                <binding name="WSHttpBinding_IService1" />
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="http://localhost:1234/Design_Time_Addresses/WcfServiceLibrary1/Service1/soap"
                binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IService1"
                contract="ServiceReference1.IService1" name="WSHttpBinding_IService1">
                <identity>
                    <dns value="localhost" />
                </identity>
            </endpoint>
        </client>
    </system.serviceModel>
</configuration>

我试过这个方法来主持: ServiceHost host = new ServiceHost(typeof(ServiceReference1。但我找不到文件Service1。(其中Service1是实现IService1合同的类文件) 如果创建仅具有SOAP类型的新项目,则Service1可见。

我应该如何通过单个应用程序托管SOAP和REST ..

1 个答案:

答案 0 :(得分:1)

最后我提出了解决方案。它真的很容易。

  1. 添加用于托管的控制台项目(或Windows窗体应用程序)
  2. 启动服务
  3. 右键单击Solution explorer中的References,然后选择Add Service Reference
  4. 提供服务路径或只需点击发现
  5. 回到主机,使用服务主机创建服务客户端和对象,即
  6. Uri baseAddress = new Uri("http://localhost:8733/Design_Time_Addresses/LoggerLibrary/Service1/");
    ServiceHost host = new ServiceHost(typeof(WcfServiceLibrary1.LoggingLibrary), baseAddress);
    host.Open();
    
    1. 事情在这里完成:)
    2. 此外,我们可以在配置文件中定义它,而不是在主机中使用Uri。此时代码将简单:

      ServiceHost host = new ServiceHost(typeof(WcfServiceLibrary1.LoggingLibrary));
      host.Open();