如何通过URI引用WCF Web服务/如何通过URI POST到WCF服务?

时间:2013-03-11 19:12:46

标签: .net wcf service web uri

我是创建Web服务的新手,我不知道如何访问我的Web服务。

我要做的是创建一个WCF Web服务,读取发布到它的JSON数据并反序列化然后执行某些操作。

我创建了一个非常简单的WCF服务,其中公开了两个方法并创建了一个uri端点。虽然当我去我的uri时我什么都没得到。

我应该可以导航到'http:// localhost:8000 / asd / EchoWithGet?s = Hello,world!'在我的浏览器中,该方法应返回“你说”+ s。当我在服务运行时导航到那个我什么也得不到。

我的问题是如何界面我的程序?我还可以通过HTML表单发布到我的服务然后打开IO阅读器吗?

提前感谢您的帮助。

以下是我的代码。

namespace WcfService1
{
[ServiceContract]
public interface IService1
{
    [OperationContract]
    [WebGet]
    string EchoWithGet(string s);

    [OperationContract]
    [WebInvoke]
    string EchoWithPost(string s);
}

public class Service1 : IService1
{
    public string EchoWithGet(string s)
    {
        return "You said " + s;
    }
    public string EchoWithPost(string s)
    {
        return "You said " + s;
    }
}

class program
{
    static void Main(string[] args)
    {
        WebServiceHost host = new WebServiceHost(typeof(Service1), new Uri("http://localhost:8000/asd/"));
        ServiceEndpoint ep = host.AddServiceEndpoint(typeof(IService1), new WebHttpBinding(), "");

        /*
        ServiceDebugBehavior sdb = host.Description.Behaviors.Find<ServiceDebugBehavior>();
        sdb.HttpHelpPageEnabled = false;
        */

        host.Open();

        Console.WriteLine("Service is running");
        Console.WriteLine("Press enter to quit...");
        Console.ReadLine();
        host.Close();
    }
}

提前感谢您的帮助

更新了,我认为我的问题源于我的配置文件。我需要在配置文件中添加哪些信息才能通过浏览器使用我的Web服务?

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

2 个答案:

答案 0 :(得分:0)

你可以修改 使用下面的代码并再次检查。

    <system.serviceModel>
  <services>     
  <service behaviorConfiguration="WcfService1.Service1Behavior"
    name="WcfService1.Service1">
    <endpoint address="" behaviorConfiguration="JSONEndpointBehavior"
      binding="webHttpBinding" contract="WcfService1.IService1" />
    <endpoint address="ws" binding="wsHttpBinding" contract="WcfService1.IService1">
    </endpoint>
    <endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />       
  </service>
</services>
<behaviors>
  <endpointBehaviors>
    <behavior name="JSONEndpointBehavior">
      <webHttp />
    </behavior>
  </endpointBehaviors>
  <serviceBehaviors>
    <behavior name="WcfService1.Service1Behavior">
      <serviceMetadata httpGetEnabled="true" />
      <serviceDebug includeExceptionDetailInFaults="false" />
    </behavior>        
  </serviceBehaviors>
</behaviors>

并使用以下代码修改webvoke

 [WebInvoke(Method = "GET", UriTemplate = "/EchoWithPost/{s}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]

string EchoWithPost(string s);

答案 1 :(得分:0)

在Web.config文件的serviceModel部分中尝试:

  <system.serviceModel>
    <services>
      <service behaviorConfiguration="RestServiceBehavior" name="Service1">
        <endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"
          bindingConfiguration="webHttpBindingWithJSONP" contract="IService1" />
      </service>
    </services>

    <bindings>
      <webHttpBinding>
        <binding name="webHttpBindingWithJSONP"/>
      </webHttpBinding>
    </bindings>

    <behaviors>
      <endpointBehaviors>
        <behavior name="web">
          <webHttp />
        </behavior>
      </endpointBehaviors>
      <serviceBehaviors>
        <behavior name="RestServiceBehavior">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="true" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"
      multipleSiteBindingsEnabled="true" />
  </system.serviceModel>