我试图在asp WCF项目中设置服务器和客户端以使用各种WCF绑定模式。
我在visual studio 2012中运行时遇到此错误:
There was no endpoint listening at http://localhost:9000/BasicHttp that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Unable to connect to the remote server
Server stack trace: at System.ServiceModel.Channels.HttpOutput.WebRequestHttpOutput.GetOutputStream() at System.ServiceModel.Channels.HttpOutput.Send(TimeSpan timeout) at System.ServiceModel.Channels.HttpChannelFactory`1.HttpRequestChannel.HttpChannelRequest.SendRequest(Message message, TimeSpan timeout) at System.ServiceModel.Channels.RequestChannel.Request(Message message, TimeSpan timeout) at System.ServiceModel.Dispatcher.RequestChannelBinder.Request(Message message, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout) at System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation) at System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)
Exception rethrown at [0]: at System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg) at System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type) at WCF_ASMX.IWCF_Service.add(Int32[] list) at WCF_ASMX._Default.NetPipeClient() in c:\xxx\Programming\c#\win_dotnet_sample_apps\WCF_ASMX_64bit_NamedPipe_F45\Default.aspx.cs:line 81 at WCF_ASMX._Default.Page_Load(Object sender, EventArgs e) in c:\xxx\Programming\c#\win_dotnet_sample_apps\WCF_ASMX_64bit_NamedPipe_F45\Default.aspx.cs:line 32 at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) at System.Web.UI.Control.OnLoad(EventArgs e) at System.Web.UI.Control.LoadRecursive() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
以下是我web.config
的相关部分:
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBinding_behaviour">
<enableWebScript />
</behavior>
<behavior name="basicHttpBinding_behaviour">
</behavior>
<behavior name="netNamedPipeBinding_behaviour">
<enableWebScript />
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service name="WCF_Sample.WCF_Service">
<endpoint
address="http://localhost:9000/BasicHttp"
behaviorConfiguration="basicHttpBinding_behaviour"
binding="basicHttpBinding"
contract="WCF_Sample.WCF_Service" />
<endpoint
address="net.pipe://localhost/NetNamedPipe"
behaviorConfiguration="netNamedPipeBinding_behaviour"
binding="netNamedPipeBinding"
contract="WCF_Sample.WCF_Service" />
</service>
</services>
</system.serviceModel>
这是我的服务:
[ServiceContract]
public interface IWCF_Service
{
[OperationContract]
int add(int[] list);
}
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(IncludeExceptionDetailInFaults = true)]
public class WCF_Service : IWCF_Service
{
[OperationContract]
[WebInvoke(Method = "GET")]
public int add(int[] list)
{
int sum = 0;
foreach(int val in list)
{
sum += val;
}
return sum;
}
}
这是我的客户:
private void Client()
{
ChannelFactory<IWCF_Service> httpFactory =
new ChannelFactory<IWCF_Service>(
new BasicHttpBinding(),
new EndpointAddress("http://localhost:9000/BasicHttp"));
ChannelFactory<IWCF_Service> pipeFactory =
new ChannelFactory<IWCF_Service>(
new NetNamedPipeBinding(),
new EndpointAddress("net.pipe://localhost/NetNamedPipe"));
IWCF_Service httpProxy = httpFactory.CreateChannel();
IWCF_Service pipeProxy = pipeFactory.CreateChannel();
string str;
str = "http: " + httpProxy.add(new int[] { 1, 2 });
Console.WriteLine(str);
str = "pipe: " + pipeProxy.add(new int[] { 1, 2 });
Console.WriteLine(str);
}
有谁知道我做错了什么?
更新以下代码: 虽然我仍然收到类似的错误:
The pipe endpoint 'net.pipe://wcf_sample/' could not be found on your local machine.
[PipeException: The pipe endpoint 'net.pipe://wcf_sample/' could not be found on your local machine. ]
[EndpointNotFoundException: There was no endpoint listening at net.pipe://wcf_sample/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.]
new web.config:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="netNamedPipeBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="netNamedPipeBehavior" name="WCF_Sample.WCF_Service">
<endpoint address="" binding="netNamedPipeBinding" bindingConfiguration=""
name="netNamedPipeEndPt" contract="WCF_Sample.IWCF_Service" />
<host>
<baseAddresses>
<add baseAddress="net.pipe://WCF_Sample" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
新客户代码:
private void Client()
{
ChannelFactory<IWCF_Service> pipeFactory =
new ChannelFactory<IWCF_Service>(
new NetNamedPipeBinding(),
new EndpointAddress(
"net.pipe://WCF_Sample"));
IWCF_Service pipeProxy = pipeFactory.CreateChannel();
string str;
str = "pipe: " + pipeProxy.add(new int[] { 1, 2 });
Console.WriteLine(str);
}
当我在浏览器中打开服务时,出现此错误:
The service class of type WCF_Sample.WCF_Service both defines a ServiceContract and inherits a ServiceContract from type WCF_Sample.IWCF_Service. Contract inheritance can only be used among interface types. If a class is marked with ServiceContractAttribute, it must be the only type in the hierarchy with ServiceContractAttribute. Consider moving the ServiceContractAttribute on type WCF_Sample.IWCF_Service to a separate interface that type WCF_Sample.IWCF_Service implements.
[InvalidOperationException: The service class of type WCF_Sample.WCF_Service both defines a ServiceContract and inherits a ServiceContract from type WCF_Sample.IWCF_Service. Contract inheritance can only be used among interface types. If a class is marked with ServiceContractAttribute, it must be the only type in the hierarchy with ServiceContractAttribute. Consider moving the ServiceContractAttribute on type WCF_Sample.IWCF_Service to a separate interface that type WCF_Sample.IWCF_Service implements.]
System.ServiceModel.Description.ServiceReflector.GetInterfaces(Type service) +12922331
System.ServiceModel.ServiceHost.CreateDescription(IDictionary`2& implementedContracts) +248
System.ServiceModel.ServiceHostBase.InitializeDescription(UriSchemeKeyedCollection baseAddresses) +146
System.ServiceModel.ServiceHost.InitializeDescription(Type serviceType, UriSchemeKeyedCollection baseAddresses) +46
System.ServiceModel.ServiceHost..ctor(Type serviceType, Uri[] baseAddresses) +146
System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(Type serviceType, Uri[] baseAddresses) +30
System.ServiceModel.Activation.ServiceHostFactory.CreateServiceHost(String constructorString, Uri[] baseAddresses) +494
System.ServiceModel.HostingManager.CreateService(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) +1434
System.ServiceModel.HostingManager.ActivateService(ServiceActivationInfo serviceActivationInfo, EventTraceActivity eventTraceActivity) +52
System.ServiceModel.HostingManager.EnsureServiceAvailable(String normalizedVirtualPath, EventTraceActivity eventTraceActivity) +598
[ServiceActivationException: The service '/WCF_Service.svc' cannot be activated due to an exception during compilation. The exception message is: The service class of type WCF_Sample.WCF_Service both defines a ServiceContract and inherits a ServiceContract from type WCF_Sample.IWCF_Service. Contract inheritance can only be used among interface types. If a class is marked with ServiceContractAttribute, it must be the only type in the hierarchy with ServiceContractAttribute. Consider moving the ServiceContractAttribute on type WCF_Sample.IWCF_Service to a separate interface that type WCF_Sample.IWCF_Service implements..]
System.Runtime.AsyncResult.End(IAsyncResult result) +489276
System.ServiceModel.Activation.HostedHttpRequestAsyncResult.End(IAsyncResult result) +178
System.ServiceModel.Activation.ServiceHttpModule.EndProcessRequest(IAsyncResult ar) +350382
System.Web.AsyncEventExecutionStep.OnAsyncEventCompletion(IAsyncResult ar) +9691825
答案 0 :(得分:1)
问题是您没有正确指定服务合同。
将contract="WCF_Sample.WCF_Service"
更改为contract="WCF_Sample.IWCF_Service" />
。
每条评论更新:
最佳做法是拥有一个接口并实现它。我将从服务库中删除属性并将它们放在服务合同上。这应该可以解决您的问题。
但是,如果您希望跳过界面,则需要从项目中完全删除IWCF_Service
并将web.config更改为:
<service behaviorConfiguration="netNamedPipeBehavior" name="WCF_Sample.WCF_Service">
<endpoint address="" binding="netNamedPipeBinding" bindingConfiguration=""
name="netNamedPipeEndPt" contract="WCF_Sample.WCF_Service" />