通过SSL的WCF服务

时间:2012-09-27 06:44:24

标签: wcf ssl

我在通过SSL设置WCF服务时遇到问题。该服务非常简单,只返回“Hello World!”。客户端可以通过HTTP接收消息,但不能通过HTTPS接收消息。我想这是我的web.config设置的问题,但我无法理解。

此外,我可以通过浏览器访问服务器上的HelloWorld.svc页面,但也失败了我的控制台程序和WCF测试客户端。

以下是错误消息:

System.ServiceModel.EndpointNotFoundException: There was no endpoint listening a
t https://fyp-pc13:997/HelloWorldService/HelloWorld.svc that could accept the me
ssage. This is often caused by an incorrect address or SOAP action. See InnerExc
eption, if present, for more details. ---> System.Net.WebException: The remote s
erver returned an error: (404) Not Found.
   at System.Net.HttpWebRequest.GetResponse()
   at System.ServiceModel.Channels.HttpChannelFactory.HttpRequestChannel.HttpCha
nnelRequest.WaitForReply(TimeSpan timeout)
   --- End of inner exception stack trace ---

WCF服务器上我的Web.config的内容:

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

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IHelloService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                  <security mode="Transport">
                    <transport realm ="" clientCredentialType="None" />
                  </security>
                        <!--<transport clientCredentialType="None" proxyCredentialType="None"
                            realm="" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>-->
                </binding>
            </basicHttpBinding>
            <wsHttpBinding>
              <binding name="WSHttpBinding_IHelloService">
                <security mode="Transport">
                  <transport realm ="" clientCredentialType="None" />
                </security>
              </binding>
            </wsHttpBinding>
        </bindings>

    <services>
        <service name="HelloWorldService">
            <!--<endpoint address="https://localhost:997/HelloWorldService/HelloWorld.svc" binding="wsHttpBinding" contract="WcfService.HelloWorld" />-->
            <endpoint address="https://fyp-pc13:997/HelloWorldService/HelloWorld.svc" binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IHelloService" contract="WcfService1.IHelloService" name="WSHttpBinding_IHelloService" />
            <endpoint address="mex" binding="mexHttpsBinding" 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 httpsGetEnabled="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>

    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />

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

</configuration>

我的ServiceReferences.ClientConfig的内容:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <system.serviceModel>
        <bindings>
            <basicHttpBinding>
                <binding name="BasicHttpBinding_IHelloService" closeTimeout="00:01:00"
                    openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
                    allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
                    maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536"
                    messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered"
                    useDefaultWebProxy="true">
                    <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
                        maxBytesPerRead="4096" maxNameTableCharCount="16384" />
                    <security mode="Transport">
                        <transport realm ="" clientCredentialType="None" />
                        <message clientCredentialType="UserName" algorithmSuite="Default" />
                    </security>
                </binding>
            </basicHttpBinding>
            <wsHttpBinding>
              <binding name="WSHttpBinding_IHelloService">
                <security mode="Transport">
                  <transport realm ="" clientCredentialType="None" />
                </security>
              </binding>
            </wsHttpBinding>
        </bindings>
        <client>
            <endpoint address="https://fyp-pc13:997/HelloWorldService/HelloWorld.svc"
              binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IHelloService"
              contract="ServiceReference1.IHelloService" name="WSHttpBinding_IHelloService">
              <identity>
                  <dns value="fyp-pc13" />
              </identity>
            </endpoint>
          <endpoint address="mex" binding="mexHttpsBinding"
              contract="IMetadataExchange" />
        </client>
    </system.serviceModel>
</configuration>

HelloWorld.svc

    <%@ ServiceHost Language="C#" Debug="true" Service="WcfService1.HelloWorld" CodeBehind="HelloWorld.svc.cs" %>

IHelloService.cs(接口)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService1
{
    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together.
    [ServiceContract]
    public interface IHelloService
    {

        [OperationContract]
        string GetData(int value);

        [OperationContract]
        CompositeType GetDataUsingDataContract(CompositeType composite);

        [OperationContract]
        string Hello();

        // TODO: Add your service operations here
    }


    // Use a data contract as illustrated in the sample below to add composite types to service operations.
    [DataContract]
    public class CompositeType
    {
        bool boolValue = true;
        string stringValue = "Hello ";

        [DataMember]
        public bool BoolValue
        {
            get { return boolValue; }
            set { boolValue = value; }
        }

        [DataMember]
        public string StringValue
        {
            get { return stringValue; }
            set { stringValue = value; }
        }
    }
}

司机(主要)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ConsoleApplication1.ServiceReference1;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                System.Net.ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
                {
                    return true;
                };
                HelloServiceClient helloServiceClient = new HelloServiceClient();
                string result = helloServiceClient.Hello();
                if (result!=null)
                {
                    Console.Out.WriteLine(result);
                }
                else
                {
                    Console.Out.WriteLine("Fail");
                }
            }
            catch (Exception e)
            {
                Console.Out.WriteLine(e.ToString());
            }

        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的客户端绑定看起来正确,但您的服务器端绑定不正确。将WSHttpBinding_IHelloService定义从客户端复制并粘贴到服务器,然后在服务器端点中引用它。

<强>更新

您的<service name="HelloWorldService">配置定义需要与完全限定的服务类名称匹配,否则激活器将无法将其与.svc文件中定义的类名匹配,即< / p>

<service name="WcfService1.HelloWorld">