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

标签 c# web-services wcf windows-services

我在“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/

相关文章:

c# - 如何访问 ASP.NET Entity Framework 中的旧实体值

c# - Nunit - 不发现测试 [无错误消息]

java - Web 服务版本控制和服务器端处理

c# - WCF:如何阻止 myServiceHost.Close() 处理 myServiceHost 对象?

wcf - wsDualHttpBinding ClientBaseAddress 和防火墙

c# - 平衡导向图

c# - 使用 XDocument 读取节点信息 - Windows Phone

java - 有效的 Java @WebMethod 返回类型是什么?

c# - Microsoft.OData.Client 在使用扩展调用函数时不加载相关实体

c# - 未处理的异常会使 WCF 服务崩溃?