【问题标题】:Simple WCF Service Error while Executing执行时出现简单的 WCF 服务错误
【发布时间】:2014-07-31 11:10:15
【问题描述】:

这可能是 WCF 中非常基本的问题。我是新手,不知道如何解决这个问题。

我从 Visual Studio 获取了 WCF 服务应用程序模板,下面是我所拥有的代码

**接口:IService1.cs **

using System.ServiceModel;
namespace WcfService1
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string GetData(int value);
    }
}

** 服务文件名:Service1.svc **

using System.ServiceModel;
namespace WcfService1
{
    public class Service1 : IService1
    {
        public string GetData(int value)
        {
            return string.Format("You entered: {0}", value);
        }
    }
}

我已将 web.config 文件编辑为

<?xml version="1.0"?>
<configuration>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehaviour">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="mexBehaviour" name="WcfService1.IService1">
        <endpoint address="Service1" binding="basicHttpBinding" contract="WcfService1.IService1"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <system.web>
    <compilation debug="true"/>
  </system.web>
</configuration>

我得到的错误是: 添加服务失败。服务元数据可能无法访问。确保您的服务正在运行并公开元数据。

详细说明:

Error: Cannot obtain Metadata from http://localhost:59790/Service1.svc If this is a Windows (R) Communication Foundation service to which you have access, please check that you have enabled metadata publishing at the specified address.  For help enabling metadata publishing, please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error    URI: http://localhost:59790/Service1.svc    Metadata contains a reference that cannot be resolved: 'http://localhost:59790/Service1.svc'.    Content Type application/soap+xml; charset=utf-8 was not supported by service http://localhost:59790/Service1.svc.  The client and service bindings may be mismatched.    The remote server returned an error: (415) Unsupported Media Type.HTTP GET Error    URI: http://localhost:59790/Service1.svc    The HTML document does not contain Web service discovery information.

由 Visual Studio 生成的原始 Webconfig 文件

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled="true"/>
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
  </system.serviceModel>
 <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>  
</configuration>

我确定这一定是简单的配置文件问题。不知道如何弄清楚。 提前感谢您的帮助。

修改 Web.config 以添加 netTcpBinding 绑定,这会在 WCF testClient 中引发错误:

<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehaviour">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service behaviorConfiguration="mexBehaviour" name="WcfService1.Service1">
        <endpoint address="Service1" binding="basicHttpBinding" contract="WcfService1.IService1"/>
        <endpoint address="Service1" binding="netTcpBinding" contract="WcfService1.IService1"/>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:8080/"/>
            <add baseAddress="net.tcp://localhost:8090/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
  </system.serviceModel>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
    <directoryBrowse enabled="true"/>
  </system.webServer>
</configuration><br>------------------------------------------------------------------


项目 2:BasicHttp 和 NetTcp 绑定托管在控制台应用程序中

我有下面的 web.config 文件(刚刚复制了一遍,以便我们一目了然)

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="HelloService.HelloService"  behaviorConfiguration="mexBehaviour">
        <endpoint address="HelloService" binding="basicHttpBinding" contract="HelloService.IHelloService"></endpoint>
        <endpoint address="HelloService" binding="netTcpBinding" contract="HelloService.IHelloService"></endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
        <host>
          <baseAddresses>
            <add baseAddress="http://localhost:59084/"/>
            <add baseAddress="net.tcp://localhost:59076/"/>
          </baseAddresses>
        </host>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="mexBehaviour">
          <serviceMetadata httpGetEnabled="True"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
  </system.serviceModel>
</configuration>

和用于托管的控制台应用程序代码

using System.ServiceModel;

namespace HelloServiceHost
{
    class Program
    {
        static void Main()
        {
            using(ServiceHost host  = new ServiceHost(typeof(HelloService.HelloService)))
            {
                host.Open();
                Console.WriteLine("Host Started");
                Console.ReadLine();
            }
        }
    }
}

错误:我在 host.Open() 方法出现错误,

 HTTP could not register URL http://+:8080/. Your process does not have access rights to this namespace (see http://go.microsoft.com/fwlink/?LinkId=70353 for details).
  • 我尝试了其他端口号,例如 53895,认为端口 8080 可能是 pre 占据。没有运气!!
  • 当我浏览这个错误时,我来到 由于我的帐户不是管理员而知道这个问题。现在我的疑问是 WCFTest Client 也在我的帐户下执行。怎么能跑 类似的代码,我不能?
  • 另外,任何建议 工作将得到认可。可能与Webconfig有关 再来一次??

扩展接口的类

using System.ServiceModel;
namespace HelloService
{
    public class HelloService : IHelloService
    {
        public string GetMessage(string Name)
        {
            return "Hello " + Name;
        }
    }
}

界面:

namespace HelloService
{
    [ServiceContract]
    public interface IHelloService
    {
        [OperationContract]
        String GetMessage(String Name);
    }
}

提前致谢!!

【问题讨论】:

  • 您打算如何使用这项服务?您只是从 URL 访问它进行测试,还是从另一个应用程序调用它?另外,您如何托管服务(IIS、网站、Windows 服务等)?配置可能是正确的,但如果服务的实例(调用和端点)没有启动,这可能会导致问题。
  • 尝试将配置文件中的合约名称更改为完全限定名称 - WcfService1.IService1,并将服务上的名称更改为 WcfService1.Service1
  • @xDaevax 抱歉,我应该这么说。我已将 Service1.svc 作为启动页面,并在 WCF 测试客户端中打开。我已经编辑了 web.config 文件,当我将 web.config 文件中的更改恢复到创建时的正常状态时,一切正常。
  • 好像只配置了元数据服务行为,没有配置实际服务?尝试添加一个额外的绑定(类似于在这里找到的:msdn.microsoft.com/en-us/library/bb332338.aspx#msdnwcfhc_topic5),看看是否有帮助。特别是,代码清单在清单 5.5
  • @Tim:感谢您的反馈伙伴。我已经更改了代码并相应地编辑了我的问题,但我仍然面临上面提到的错误。

标签: .net wcf wcf-configuration


【解决方案1】:

有时 system.web 部分下的以下 web.config 行可能会导致您的开发机器出现问题

&lt;identity impersonate="true" /&gt;

【讨论】:

    【解决方案2】:

    web.config 中的所有内容看起来都不错,但有一个例外: 下面一行中的“名称”应该是“WcfService1.Service1”而不是“WcfService1.IService1”:

    <service behaviorConfiguration="mexBehaviour" name="WcfService1.Service1">
    

    我的网络配置如下,它适用于我:

    <?xml version="1.0"?>
    <configuration>    
      <appSettings>
        <add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
      </appSettings>
      <system.web>
        <compilation debug="true" targetFramework="4.5" />
        <httpRuntime targetFramework="4.5"/>
      </system.web>  
      <system.serviceModel>
        <behaviors>
          <serviceBehaviors>
            <behavior name="mexBehaviour">
              <serviceMetadata httpGetEnabled="True"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <services>
          <service behaviorConfiguration="mexBehaviour" name="WcfService1.Service1">
            <endpoint address="Service1" binding="basicHttpBinding" contract="WcfService1.IService1"/>
            <host>
              <baseAddresses>
                <add baseAddress="http://localhost:8080/"/>
              </baseAddresses>
            </host>
          </service>
        </services>
      </system.serviceModel>
      <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
        <directoryBrowse enabled="true"/>
      </system.webServer>
    </configuration>
    

    【讨论】:

    • 感谢@Uriil,感谢您发现并修复了该问题。 :)
    • @Uriil : 好棒的伙伴。它真的很有帮助。我接受了你的回答。我尝试添加 netTcpBinding 绑定和 mexHttpBinding,现在它在 ECF 测试客户端中抛出错误。你能帮帮我吗?谢谢
    • @Uriil :我尝试实现 BasicHttp 以及在 Colsole 应用程序中托管的 netTcpBinding 和 mexHttpBinding。但是我仍然面临一些问题。在我的问题的“项目 2:控制台应用程序中托管的 BasicHttp 和 NetTcp 绑定”下突出显示。我试图浏览我最好的答案,但没有运气。请问你能帮忙吗?谢谢!!
    • 您应该创建新问题,发布一些代码。在 cmets 很难帮你
    猜你喜欢
    • 2016-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-17
    • 2012-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多