我试图创建基本的WCF服务,并将其托管在控制台应用程序中。
这是我的WCF项目代码。
ISampleService.cs
using System.ServiceModel;
namespace MultipleSeviceContractAppl
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "ISampleService" in both code and config file together.
[ServiceContract]
public interface ISampleService
{
[OperationContract]
string DoWork();
}
}SampleService.cs
namespace MultipleSeviceContractAppl
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "SampleService" in both code and config file together.
public class SampleService : ISampleService
{
public string DoWork()
{
return "Message from WCFservice";
}
}
}App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="MultipleSeviceContractAppl.SampleService" behaviorConfiguration="mexBehaviour">
<endpoint address="SampleService" binding="netTcpBinding" contract="MultipleSeviceContractAppl.ISampleService">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/"/> <!--For metadata exchange-->
<add baseAddress="net.tcp://localhost:8737/" /> <!--Endpoint, netTCP binding, For data exchange-->
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>WCF托管在控制台appl - Program.cs上
using System;
using System.ServiceModel;
namespace ConsumeWCFApplicationAppl
{
class Program
{
static void Main()
{
using (ServiceHost host = new ServiceHost(typeof(MultipleSeviceContractAppl.SampleService)))
{
host.Open();
Console.WriteLine("Host started @" + DateTime.Now.ToString());
Console.ReadKey();
}
}
}
}在控制台应用程序的host.Open();行中,引发了以下异常。
System.InvalidOperationException类型的未处理异常出现在System.ServiceModel.dll附加信息中:服务'MultipleSeviceContractAppl.SampleService‘有零个应用程序(非基础结构)端点。这可能是因为没有为您的应用程序找到配置文件,或者因为在配置文件中找不到匹配服务名称的服务元素,或者因为在服务元素中没有定义端点。帮我找出我的错误。谢谢
发布于 2018-01-31 23:51:51
您需要在控制台应用程序中复制配置,并将对服务模型程序集的DLL的引用添加到此项目.
<behaviors>
<serviceBehaviors>
<behavior name="mexBehaviour">
<serviceMetadata httpGetEnabled="false"/>
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="MultipleSeviceContractAppl.SampleService" behaviorConfiguration="mexBehaviour">
<endpoint address="SampleService" binding="netTcpBinding" contract="MultipleSeviceContractAppl.ISampleService">
</endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/"/> <!--For metadata exchange-->
<add baseAddress="net.tcp://localhost:8737/" /> <!--Endpoint, netTCP binding, For data exchange-->
</baseAddresses>
</host>
</service>
</services>https://stackoverflow.com/questions/48551018
复制相似问题