如何在IIS上托管WCF服务(没有Visual Studio)?

时间:2017-06-15 02:26:16

标签: wcf iis-7.5 wcf-rest

我使用visual studio 2013创建了我的第一个WCF休息服务。当我从visual studio运行它时它非常有效。

但现在我需要在服务器上将其部署到iis。我无法使用visual studio将其推送到IIS服务器,因为它位于差异域上(安全问题)。

所以我已将该服务发布到一个usb棒中。

在IIS中创建了一个名为“HostedcGService' enter image description here

我将文件复制到物理路径文件夹中。 但是当我尝试使用以下URL访问服务时

http://localhost:4567/HostedWcfService/data/10 enter image description here

我的代码如下, App.config文件如下,

 <?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="svcBehavior">        
          <serviceMetadata httpGetEnabled="true"/>         
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <bindings>
      <webHttpBinding>
        <binding>
          <security mode="Transport"/>
        </binding>
      </webHttpBinding>
    </bindings>
    <services>
      <service name="WcfJsonRestService.Service1" behaviorConfiguration="svcBehavior">
        <endpoint name="" binding="webHttpBinding" address="" contract="WcfJsonRestService.IService1" />        
      </service>
    </services>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

IService1.cs

namespace WcfJsonRestService
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        Service1.Person GetData(string id);
    }
}

Service1.cs

namespace WcfJsonRestService
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
    public class Service1 : IService1
    {
        [WebInvoke(Method = "GET",
            ResponseFormat = WebMessageFormat.Json,
            UriTemplate = "data/{id}")]
        public Person GetData(string id)
        {
            // lookup person with the requested id 
            return new Person()
            {
                Id = Convert.ToInt32(id),
                Name = "Leo Messi"
            };
        }

        public class Person
        {
            public int Id { get; set; }
            public string Name { get; set; }
        }
    }
}

还有一个web.config文件..我需要吗? appconfig和webconfig之间的区别是什么?

网络配置

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

  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <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="WcfJsonRestService.Service1">
        <endpoint address="http://localhost:4567/HostedWcfService"

                  binding="webHttpBinding"

                  contract="WcfJsonRestService.IService1"/>
      </service>
    </services>
    <behaviors>
      <endpointBehaviors>
        <behavior>
          <webHttp />
        </behavior>
      </endpointBehaviors>
    </behaviors>
  </system.serviceModel>


</configuration>

0 个答案:

没有答案