我正在尝试将 WCF 服务放入 Windows 服务中。我使用 svcutil 来使用 CalculatorService.cs 和 output.config。我把output.config的内容放到App.config里,写了一个客户端,把CalculatorService.cs也放进去。当它运行时,我收到错误消息“无法在 ServiceModel 客户端配置部分找到引用契约(Contract)‘IAnswerResult’的默认端点元素”。在我的客户端中,我刚刚复制了向导所说的使用内容:
protected void Page_Load(object sender, EventArgs e)
{
AnswerResultClient client = new AnswerResultClient(); // error here
client.QuestionAnswered("1", "2", "3");
}
在客户端(生成的)的 App.config 中,我有以下内容,但它告诉我契约(Contract)“IAnswerResult”无效。不知道为什么……是svcutil生成的。
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IAnswerResult" />
</basicHttpBinding>
<netTcpBinding>
<binding name="NetTcpBinding_IAnswerResult" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="http://localhost:6255/CalculatorService" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IAnswerResult" contract="IAnswerResult"
name="BasicHttpBinding_IAnswerResult" />
<endpoint address="net.tcp://localhost:6256/CalculatorService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IAnswerResult"
contract="IAnswerResult" name="NetTcpBinding_IAnswerResult">
<identity>
<servicePrincipalName value="host/Server2012" />
</identity>
</endpoint>
</client>
</system.serviceModel>
Windows 服务/WCF 服务中的代码:
namespace EternaService
{
[ServiceContract(Namespace = "http://EternaService")]
public interface IAnswerResult
{
[OperationContract]
string QuestionAnswered(string person_int, string course_int, string lesson_int);
}
public class CalculatorService : IAnswerResult
{
public string QuestionAnswered(string person_int, string course_int, string lesson_int)
{
string result = "";
// Have input, now do something with it.
return result;
}
}
public partial class EternaService : ServiceBase
{
public ServiceHost serviceHost = null;
public EternaService()
{
this.CanStop = true;
this.CanHandlePowerEvent = true;
this.CanHandleSessionChangeEvent = true;
this.CanPauseAndContinue = true;
this.CanShutdown = true;
InitializeComponent();
}
protected override void OnStart(string[] args)
{
if (serviceHost != null)
{
serviceHost.Close();
}
serviceHost = new ServiceHost(typeof(CalculatorService));
serviceHost.Open();
}
Windows 服务/WCF 服务中的 App.config:
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<services>
<service name="EternaService.CalculatorService" behaviorConfiguration="myServiceBehave">
<host>
<baseAddresses>
<add baseAddress="http://localhost:6255/CalculatorService"/>
<add baseAddress="net.tcp://localhost:6256/CalculatorService"/>
</baseAddresses>
</host>
<endpoint address="http://localhost:6255/CalculatorService" binding="basicHttpBinding" contract="EternaService.IAnswerResult" />
<endpoint address="net.tcp://localhost:6256/CalculatorService" binding="netTcpBinding" contract="EternaService.IAnswerResult" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<endpoint address="mex" binding="mexTcpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="myServiceBehave">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
非常感谢任何方向。这是我的第一次尝试,我迷路了!
编辑: 我将 Windows 服务 ServiceContract 命名空间更改为:
[ServiceContract(Namespace = "http://CADEEternaService")]
在客户端 CalculatorService 类中:
[System.ServiceModel.ServiceContractAttribute(Namespace= "http://CADEEternaService", ConfigurationName="IAnswerResult")]
public interface IAnswerResult
{
[System.ServiceModel.OperationContractAttribute(Action= "http://CADEEternaService/IAnswerResult/QuestionAnswered", ReplyAction= "http://CADEEternaService/IAnswerResult/QuestionAnsweredResponse")]
string QuestionAnswered(string person_int, string course_int, string lesson_int);
[System.ServiceModel.OperationContractAttribute(Action= "http://CADEEternaService/IAnswerResult/QuestionAnswered", ReplyAction= "http://CADEEternaService/IAnswerResult/QuestionAnsweredResponse")]
System.Threading.Tasks.Task<string> QuestionAnsweredAsync(string person_int, string course_int, string lesson_int);
}
在客户端的 app.config 中:
<endpoint address="http://localhost:6255/CalculatorService" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IAnswerResult" contract="CADEEternaService.IAnswerResult"
name="BasicHttpBinding_IAnswerResult" />
<endpoint address="net.tcp://localhost:6256/CalculatorService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IAnswerResult"
contract="CADEEternaService.IAnswerResult" name="NetTcpBinding_IAnswerResult">
但它仍然将“CADEEternaService.IAnswerResult”标记为错误,我也收到了原始错误“无法找到引用契约(Contract)‘IAnswerResult’的默认端点元素”……但是,我觉得奇怪的是在错误消息中对 IAnswerResult 而不是 CADEEternaService.IAnswerResult 咆哮。
最佳答案
您需要在契约(Contract)之前添加生成的服务引用的命名空间.... contract="xxxxxxx.IAnswerResult
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IAnswerResult" />
</basicHttpBinding>
<netTcpBinding>
<binding name="NetTcpBinding_IAnswerResult" />
</netTcpBinding>
</bindings>
<client>
<endpoint address="http://localhost:6255/CalculatorService" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IAnswerResult" contract="**xxxxx.IAnswerResult**"
name="BasicHttpBinding_IAnswerResult" />
<endpoint address="net.tcp://localhost:6256/CalculatorService"
binding="netTcpBinding" bindingConfiguration="NetTcpBinding_IAnswerResult"
contract="IAnswerResult" name="NetTcpBinding_IAnswerResult">
<identity>
<servicePrincipalName value="host/Server2012" />
</identity>
</endpoint>
</client>
关于c# - 客户端在 Windows 服务中找不到 WCF 的端点元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48530263/