gpt4 book ai didi

c# - 在 Windows 服务中使用 WCF 服务时出错

转载 作者:太空宇宙 更新时间:2023-11-03 13:10:50 24 4
gpt4 key购买 nike

我在“Windows 服务”中添加了 wcf 服务作为服务引用。但是当我尝试创建实例时,它只会给我以下错误

"Could not find default endpoint element that references contract 'ServiceReference1.IClientService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element."

代码

            pushNotification push_notification = new pushNotification();
var proxy = new ServiceReference1.ClientServiceClient();

using (var scope = new OperationContextScope(proxy.InnerChannel))
{
// Add a HTTP Header to an outgoing request

HttpRequestMessageProperty requestMessage = new HttpRequestMessageProperty();
requestMessage.Headers["Authorization"] = "Push Notification";
OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = requestMessage;
push_notification = proxy.GetPendingNotification();
}

app.config

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:4567/ClientService.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IClientService"
name="BasicHttpBinding_IService1" behaviorConfiguration="webHttpBehavior" />
</client>
<behaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>

最佳答案

确保将端点信息放入主 App.config 或 Web.config 中。如果您在单独的项目中添加了服务引用,则需要复制配置:

    <system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IService1" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:4567/ClientService.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IClientService"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>

在你的启动项目App.config/Web.config!

关于c# - 在 Windows 服务中使用 WCF 服务时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28762272/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com