无法运行我的wcf服务

时间:2014-03-31 01:57:14

标签: c# asp.net web-services wcf

我已经创建了wcf服务,但是当我运行时,它会显示这样的错误。

enter image description here 我使用重命名工具更改了名称类和接口名称。 这是service.cs类的代码

public class MyTest : MyServices
{
    public string MyTask1(string a)
    {
        return "Hello " + a;
    }
    public string MyTask2(DataContract1 dc)
    {
        return "Hello " + dc.fname;
    }
}

以下是我的界面代码:

[ServiceContract]
public interface MyServices
{

    [OperationContract]
    string MyTask1(string myValue);
    [OperationContract]
    string MyTask2(DataContract1 dcValue);

}

// Use a data contract as illustrated in the sample below to add composite types to service operations.
[DataContract]
public class DataContract1
{
    string firstName;
    string lastName;
    [DataMember]
    public string fname
    {
        get { return firstName; }
        set { firstName = value; }
    }
    public string lname
    {
        get { return lastName; }
        set { lastName = value; }
    }
}

我编辑了我的web.config文件,并添加了这些行(我在黑皮书中读到)

<service name="Service" behaviorConfiguration="ServiceBehavior">
        <!--Service EndPoints-->
        <endpoint address="" binding="wsHttpBinding" contract="MyServices">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadatExchange"/>
      </service>

这是我的system.servicemodel代码

<system.serviceModel>
    <services>
      <!-- My Custimization -->
      <service name="Service" behaviorConfiguration="ServiceBehavior">
        <!--Service EndPoints-->
        <endpoint address="" binding="wsHttpBinding" contract="MyServices">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
        <endpoint address="mex" binding="mexHttpBinding" contract="IMetadatExchange"/>
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>

2 个答案:

答案 0 :(得分:1)

1)在遗留Web服务中,我们使用wsdl创建代理。 WSDL公开Web服务元数据。在wcf另一个术语是mex端点,它也暴露元数据,但wsdl仍然存在于wcf.i是wcf的新内容因此我混淆了wsdl&amp;和wsdl之间的区别。 mex端点?

这是完全相同的事情,但mex旨在支持非HTTP协议和高级配置/安全方案。 WSDL是传统方式,MEX是使用WCF的新改进版本。

2)httpGetEnabled =“false”或httpGetEnabled =“true”是什么意思

即使您没有为您的服务定义mex端点,它也会通过defautl url通过wsdl公开元数据。

3)如果我设置httpGetEnabled =“false”那么会发生什么?这是否意味着客户端将无法从IDE添加服务引用?但我设置httpGetEnabled =“false”,看到客户端可以添加服务引用。 httpGetEnabled是假的还是真的,这对我来说非常困惑?

只有在启用httpGetEnabled / httpsGetEnabled或者在服务配置中定义了mex端点时,客户端才能在VS中添加引用。最佳做法是在开发环境中公开元数据,而不是在生产环境中公开元数据。您还可以通过单独的程序集分发服务合同,并使用ChannelFactory。

4)一个人说: - MEX和WSDL是两种不同的方案,可以告诉潜在客户你的服务结构。因此,您可以选择将服务合同公开为(MEX)或WSDL。如果上述说法属实,请告诉我何时使用MEX&amp;何时使用WSDL?

WSDL通常通过您无法真正配置的http或https get url公开(例如出于安全限制或向后兼容性)。 MEX端点通过可配置端点公开元数据,并且可以使用不同类型的传输,例如TCP或HTTP,以及不同类型的安全机制。

因此,MEX更易于配置,而WSDL与使用WSDL的旧版客户端和非.NET客户端更具互操作性。

5)如何禁用mex并仅通过WSDL公开我的服务

请勿在配置中指定mex端点并使用httpGetEnabled

6)WSDL支持所有出价,如wshttp,wsdualhttp或tcp等......

公开元数据与调用服务完全不同。

更新

你试图表示配置中应该没有与mex端点相关的条目,httpgetenable看起来像

是的,您不必指定mex端点AND httpGetEnabled。只需要一个公开元数据。不要指定httpGetUrl,因为这取决于您的托管环境。

你说mex是可配置的,但是wsdl不是。你试图表示mex是可配置的......请讨论mex支持和配置的配置。如何配置。

MEX端点是允许客户端使用SOAP消息而不是http get请求来接收服务元数据的特殊端点。您可以创建可通过http,https,tcp甚至命名管道访问的MEX端点。 HttpGetEnable允许您通过HTTP GET方法公开元数据,通常是服务的地址,后缀为“?wsdl”

MEX和WSDL都输出几乎相同的东西。

在大多数情况下,不需要MEX端点 - 使用带有http get的WSDL通常就足够了。

我理解你有意理解这一部分,但不要花太多时间来讨论这个问题:还有很多其他复杂的功能!

答案 1 :(得分:0)

拼写错误我猜...

IMetadatExchange -- > IMetadataExchange

     <endpoint address="mex" binding="mexHttpBinding" contract="IMetadatExchange"/>

试试这个......

<system.serviceModel>
    <services>
      <!-- My Custimization -->
      <service name="Service" behaviorConfiguration="ServiceBehavior">
        <!--Service EndPoints-->
        <endpoint address="" binding="wsHttpBinding" contract="MyServices">
          <identity>
            <dns value="localhost"/>
          </identity>
        </endpoint>
           <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
      </service>
    </services>
    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceBehavior">
          <!-- To avoid disclosing metadata information, set the values below to false before deployment -->
          <serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
          <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
          <serviceDebug includeExceptionDetailInFaults="false"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="basicHttpsBinding" scheme="https"/>
    </protocolMapping>
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
  </system.serviceModel>
相关问题