Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
447 views
in Technique[技术] by (71.8m points)

c# - Expose webHttpBinding endpoint in a WCF service

I created a WCF service and exposed three endpoints which are basicHttpBinding, wsHttpBinding and webHttpBinding. This is a test service for my experiments with WCF. But, whenever I add service reference using the .svc file, I only get two (basic and ws) endpoints. There doesn't seem to be a third (webHttpBidning) endpoint being exposed for some reason.

To reproduce this issue, create a WCF application project, delete the Service1 service, add new item > WCF service named TestService, and change the config file to the following :

<system.serviceModel>
<services>
  <service name="WcfTestService.TestService" behaviorConfiguration="TestServiceBehavior">
    <host>
      <baseAddresses>
        <add baseAddress="http://localhost/WcfTestService/TestService.svc"/>
      </baseAddresses>
    </host>
    <endpoint address="basic"
              binding="basicHttpBinding"
              contract="WcfTestService.ITestService" />
    <endpoint address="ws"
              binding="wsHttpBinding"
              contract="WcfTestService.ITestService" />
    <endpoint address="web"
              binding="webHttpBinding"
              contract="WcfTestService.ITestService" />
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>
<behaviors>
  <serviceBehaviors>
    <behavior name="TestServiceBehavior">
      <serviceMetadata httpGetEnabled="true" />
    </behavior>
  </serviceBehaviors>
  <endpointBehaviors>
    <behavior name="webBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
</behaviors>

Here is the code for ITestService.cs:

[ServiceContract]
public interface ITestService
{
    [OperationContract]
    [WebInvoke]
    Boolean ValidateUser(User user);
}

[DataContract]
public class User
{
    [DataMember(Name = "Name")]
    public String UserName { get; set; }
    [DataMember]
    public String Password { get; set; }
}

and for TestService.svc

public class TestService : ITestService
{
    public bool ValidateUser(User user)
    {
        if (user.UserName == "User" && user.Password == "123")
        {
            return true;
        }
        return false;
    }
}

I tried different combination of WebInvoke parameters and WebGet parameters, but failed.

Can anyone tell me why the third endpoint is not showing up in the WSDL file?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

@decyclone: I have successfully exposed webHttpBindings without any issue. But I found some interesting thing when it get exposed and when it not!

I can see web binding getting exposed in Wcf Test Client.

Here are my configurations

<services>
  <service behaviorConfiguration="TestWeb.Service2Behavior" name="TestWeb.Service2">
    <endpoint address="" binding="wsHttpBinding" contract="TestWeb.Service2">
      <identity>
        <dns value="localhost" />
      </identity>
    </endpoint>
    <endpoint address="web" binding="webHttpBinding" contract="TestWeb.Service2">
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
  </service>
</services>

Point to note here is, its working fine using VS2008 with Framework 3.5, VS2010 with Framework 3.5, when I use VS2010 and Framework 4.0 then I can't see WebHttpBinding getting exposed in WCF Test Client, however I can use that binidng to do http post in all cases.

I assume that in Framework 4.0 its not visible by default, even I try enable endpointDiscovery but still no luck!

I have covered this behaviour in my post


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...