在Windows服务中使用WCF服务时出错

时间:2015-02-27 10:00:43

标签: c# web-services wcf windows-services

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

  

“无法找到引用合同的默认端点元素   ServiceModel客户端中的“ServiceReference1.IClientService”   配置部分。这可能是因为没有配置文件   找到您的应用程序,或者因为没有端点元素匹配   这份合同可以在客户要素中找到。“

代码

            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>

1 个答案:

答案 0 :(得分:1)

确保将端点信息放在主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