我在通过 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());
}
}
}
}