Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用SSL在https上托管WCF_C#_Wcf_Ssl_Iis 7 - Fatal编程技术网

C# 使用SSL在https上托管WCF

C# 使用SSL在https上托管WCF,c#,wcf,ssl,iis-7,C#,Wcf,Ssl,Iis 7,我创建了一个ASP.NET项目,并添加了一个名为RestService.svc的WCF服务。以下是RestService.svc.cs中的代码 [ServiceBehavior(IncludeExceptionDetailInFaults = true), AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)] public class RestSe

我创建了一个ASP.NET项目,并添加了一个名为RestService.svc的WCF服务。以下是RestService.svc.cs中的代码

 [ServiceBehavior(IncludeExceptionDetailInFaults = true), AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class RestService : IRestService
    {
        public string SayHello()
        {
            return "Hello!!!";
        }
    }
这是服务合同IRestService

[ServiceContract]
public interface IRestService
{

    [OperationContract]
    [WebGet(ResponseFormat = WebMessageFormat.Json, UriTemplate = "SayHello")]
    string SayHello();
}
我的配置文件如下:

<?xml version="1.0"?>
<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />

<customErrors mode="Off" defaultRedirect="default.aspx">
            <error statusCode="403" redirect="NoAccess.htm" />
            <error statusCode="404" redirect="FileNotFound.htm" />
        </customErrors>
  </system.web>


 <system.serviceModel>
    <services>
      <service name="MyService.RestService" behaviorConfiguration="httpsBehavior">
        <endpoint address=""
                  binding="basicHttpBinding"
                  bindingConfiguration="webHttpBinding"
                  contract="MyService.IRestService"/>

        <endpoint address="mex"
                  binding="mexHttpsBinding"
                  contract="IMetadataExchange" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="webHttpBinding">
          <security mode="Transport">
            <transport clientCredentialType="None"/>
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <behaviors>
      <serviceBehaviors>
        <behavior name="httpsBehavior">
          <serviceMetadata httpsGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="true"/>
        </behavior>
      </serviceBehaviors>

    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" aspNetCompatibilityEnabled="true" />
  </system.serviceModel>


 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>

</configuration>

当我在浏览器中请求.svc文件时,它将返回以下消息: REST服务

您已经创建了一项服务。

要测试此服务,您需要创建一个客户机并使用它调用该服务。可以使用命令行中的svcutil.exe工具,使用以下语法执行此操作:


web应用程序使用端口15001上的SSL托管在IIS中。我还创建了一个aspx页面,看看我是否可以通过https访问该网站,并且它可以正常工作。但当我调用该服务时,它在浏览器中显示一个黑色页面,在Chrome Rest Client中显示400:Bad Request消息。

如果您需要Rest服务,我建议使用更适合此目的的web api。WCF工作在一个称为SOAP的基于消息的协议上,该协议可以应用于HTTP调用。当WCF返回消息时,您使用的URL是什么?如果您的请求以“.svc”结尾,那么它是预期的,因为它不会被路由到您定义的方法,而是一个解释WCF用法的默认页面。https://example.com:15001/RestService.svc/SayHello是我正在使用的URL,它给出一个空白页面。(故意在这里留下一个空格,这样它就不会被格式化)如果您需要REST服务,我建议使用更适合此目的的web api。WCF工作在一个称为SOAP的基于消息的协议上,该协议可以应用于HTTP调用。当WCF返回消息时,您使用的URL是什么?如果您的请求以“.svc”结尾,那么它是预期的,因为它不会被路由到您定义的方法,而是一个解释WCF用法的默认页面。https://example.com:15001/RestService.svc/SayHello是我正在使用的URL,它给出一个空白页面。(故意在此处留下空格,以免格式化)