执行时的简单WCF服务错误

时间:2014-06-10 15:40:42

标签: .net wcf wcf-configuration

这可能是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 Binding,这会在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可能是预先的 占据。没运气!!
  • 当我浏览这个错误时,我来了 由于我的帐户不是管理员,因此知道此问题。现在我怀疑了 WCFTest客户端也在我的帐户下执行。怎么运行 类似的代码,我可以&#39;
  • 此外,任何建议,以此 工作会得到满足。可能与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);
    }
}

提前致谢!!

2 个答案:

答案 0 :(得分:4)

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>

答案 1 :(得分:0)

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

<identity impersonate="true" />