简体   繁体   中英

Unable to run my wcf service

I have created a wcf service, but when I run , then it show error like this.

在此处输入图片说明 I have change the name class and interface name using rename tool. Here is code for service.cs class

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

Here is the code for my interface :

[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; }
    }
}

I have edited my web.config file, and added these lines (I read it in black book)

<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>

Here is my system.servicemodel code

<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>

1) in legacy web service we create proxy using wsdl. WSDL expose web service meta data. in wcf another term comes that mex endpoint which also expose meta data but wsdl is still live in wcf.i am new in wcf hence i am confusing what is the difference between wsdl & mex endpoint?

It's pretty the same thing but mex is designed to support non-HTTP protocols and for advanced configuration/security scenarios. WSDL is the legacy way and MEX is the new improved version with WCF.

2) what is the meaning of httpGetEnabled="false" or httpGetEnabled="true"

It will expose metadata via wsdl through the defautl url, even if you don't have defined a mex endpoint for your service.

3) if i set httpGetEnabled="false" then what will happen? does it mean that client will not be able to add service reference from IDE? but i set httpGetEnabled="false" and saw client can add service reference. so it is very confusing for me that what httpGetEnabled is false or true does ?

A client can add a reference in VS only if httpGetEnabled/httpsGetEnabled is enable or if you have define a mex endpoint in the configuration of your service. The best practice is to expose metadata on dev environnement but not on production. You can also distribute your service contracts via separate assemblies and use ChannelFactory.

4) one guy said :- MEX and WSDL are two different schemes to tell potential clients about the structure of your service. So you can choose to either make your service contracts public as (MEX) or WSDL. if the above statement is true then tell me when to use MEX & when to use WSDL?

A WSDL is generally exposed through http or https get urls that you can't really configure (say for security limitations or for backward compatibility). MEX endpoints expose metadata over configurable endpoints, and can use different types of transports, such as TCP or HTTP, and different types of security mechanisms.

So MEX are more configurable, while WSDL is more interoperable with older versions of clients and non-.net clients that work with WSDLs.

5) how could i disable mex and expose my service through only WSDL

Do not specifiy a mex endpoint in your config and use httpGetEnabled

6) WSDL support all bidning like wshttp,wsdualhttp or tcp etc...

Exposing metadata is totally different that invoking the service.

UPDATE

re you try to mean that there should be no mex endpoint related entry in config and httpgetenable would look like

Yes, you don't have to specify a mex endpoint AND httpGetEnabled. Only one is required to expose metadata. Do not specifiy httpGetUrl as this is depending on your hosting environment.

you said mex is configurable but wsdl is not. what ru trying to means mex is configurable...please discuss what kind of configuration mex support & how to configure.

MEX endpoints are special endpoints that allow clients to receive the service's metadata by using SOAP messages instead of http get requests. You can create MEX endpoint that can be accessed through http, https, tcp, and even named pipes. HttpGetEnable allow you to expose metadata through HTTP GET method, usually the service's address with the suffix of '?wsdl'

MEX and WSDL both output nearly the same thing.

In most cases there is no need for MEX endpoint – using WSDLs with http get is usually enough.

I understand your intention to understand this part, but do not spend to many times on this : there are so many others complicated features !

Spelling mistake I guess..

IMetadatExchange -- > IMetadataExchange

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

try this...

<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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM