WCF错误 - 找不到引用合同"UserService.UserService"的默认端点元素

Not*_*Dan 96 .net c# wcf

任何想法如何解决这一问题?

UserService.UserServiceClient userServiceClient = new UserServiceClient();
            userServiceClient.GetUsersCompleted += new EventHandler<GetUsersCompletedEventArgs>(userServiceClient_GetUsersCompleted);
            userServiceClient.GetUsersAsync(searchString);
Run Code Online (Sandbox Code Playgroud)

.

<system.serviceModel>
    <bindings>
        <basicHttpBinding>
            <binding name="BasicHttpBinding_UserService" 
                     maxBufferSize="2147483647" 
                     maxReceivedMessageSize="2147483647">
                <security mode="None" />
            </binding>
        </basicHttpBinding>
    </bindings>
    <client>
        <endpoint address="http://localhost:52185/UserService.svc" 
                  binding="basicHttpBinding" 
                  bindingConfiguration="BasicHttpBinding_UserService" 
                  contract="UserService.UserService"
                  name="BasicHttpBinding_UserService" />
    </client>
    <behaviors>
        <serviceBehaviors>
            <behavior name="Shell.Silverlight.Web.Service3Behavior">
                <serviceMetadata httpGetEnabled="true" />
                <serviceDebug includeExceptionDetailInFaults="false" />
            </behavior>
        </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
    <services>
        <service behaviorConfiguration="Shell.Silverlight.Web.Service3Behavior" 
                 name="Shell.Silverlight.Web.Service3">
            <endpoint address="" 
                      binding="basicHttpBinding" 
                      contract="Shell.Silverlight.Web.Service3" />
            <endpoint address="mex" 
                      binding="mexHttpBinding" 
                      contract="IMetadataExchange" />
        </service>
    </services>
</system.serviceModel>
Run Code Online (Sandbox Code Playgroud)

无法在ServiceModel客户端配置部分中找到引用合同"UserService.UserService"的默认端点元素.这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素.

解决!

我没有提到这是一个Silverlight应用程序.我在DLL中有wcf引用,它有自己的"ServiceReferences.ClientConfig"文件.我将DLL的ServiceReferences.ClientConfig的内容移动到主Silverlight项目并且它工作了.

spr*_*ite 185

我遇到了同样的问题.我的应用程序也是一个Silverlight应用程序,该服务是从类库中调用的,其中包含正在使用的自定义UserControl.

解决方案很简单.将端点定义从类库的配置文件(例如ServiceReferences.ClientConfig)复制到silverlight应用程序的配置文件.我知道你希望它能够在不必这样做的情况下工作,但显然雷蒙德的某个人当天有个假期.

  • 此解决方案也适用于ASP.NET和MVC项目.如果将服务添加到类库中,它将无法使用该框,从库项目中的app.config获取system.serviceModel部分并将其放入web.config中. (50认同)
  • 看起来这对任何类型的项目都是如此,我使用quartz.net作为Windows服务,并且在引用Web服务的不同程序集上有工作.它对我也有用.谢谢. (5认同)
  • 雷蒙德度假玩笑+1 ...无论如何,有没有办法将它添加为外部文件,而不是使用`web.config`? (3认同)
  • 嗨,它也适用于Windows Phone项目.正如@thiagoleite所说,它似乎适用于大多数项目类型. (2认同)
  • 仍在拯救生命 (2认同)

Vis*_*hal 44

您还可以在类库中以编程方式设置这些值,这样可以避免配置文件在库中不必要地移动.简单的BasciHttpBinding的示例代码是 -

BasicHttpBinding basicHttpbinding = new BasicHttpBinding(BasicHttpSecurityMode.None);
basicHttpbinding.Name = "BasicHttpBinding_YourName";
basicHttpbinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None;
basicHttpbinding.Security.Message.ClientCredentialType = BasicHttpMessageCredentialType.UserName;

EndpointAddress endpointAddress = new EndpointAddress("http://<Your machine>/Service1/Service1.svc");
Service1Client proxyClient = new Service1Client(basicHttpbinding,endpointAddress);
Run Code Online (Sandbox Code Playgroud)


Mik*_*ill 12

以防万一在使用WPF(而不是WCF或Silverlight)时遇到同样的问题:

连接到Web服务时出现此错误.当我的代码在"主要"WPF应用程序解决方案中,没有问题,它完美地工作.但是当我将代码移动到更合理的DAL层解决方案时,它会抛出异常.

无法在ServiceModel客户端配置部分中找到引用合同"MyWebService.MyServiceSoap"的默认端点元素.这可能是因为没有为您的应用程序找到配置文件,或者因为在客户端元素中找不到与此合同匹配的端点元素.

正如此线程中"Sprite"所述,您需要手动复制标记.

对于WPF应用程序,这意味着复制从标签的app.config在我DAL解决的app.config主WPF应用程序的解决方案.

  • @sprite:我会添加一个注释,但StackOverflow注释总是显示为一个长段.对于像我一样的5段回复,这将导致一个冗长,难以理解,不友好的段落.写一个单独的回复让我给出了一个更清晰的答案,这更有可能帮助其他用户(让我们面对它,这是这样的网站的重点) (2认同)

Dal*_*e C 6

我遇到了同样的问题,无论出于什么原因,当我第一次添加服务时,Visual Studio没有更新Web配置.我发现更新服务参考也解决了这个问题.

脚步:

  1. 导航到服务参考文件夹
  2. 展开它
  3. 右键单击并选择更新服务参考
  4. 观察Web配置更新