如何使用WCF服务生成XML输出?

4 c# wcf web-services

我已经设置了以下界面.

[ServiceContract]
public interface IService1
{
  [OperationContract]
  String Ping();
}
Run Code Online (Sandbox Code Playgroud)

其实施如下.

public class Service1 : IService1
{
  public string Ping(){ return "Pong"; }
}
Run Code Online (Sandbox Code Playgroud)

根据VS中的测试应用程序,它在调用时正常工作.我的问题是当我输入http:// localhost:12345/Service1.svc(或者可能是Service1.svc?PingService.svc/Ping)时,我希望文本显示在屏幕上).是完全关闭还是我吠叫正确的树?

当然," Pong "最终将成为XML结构.

编辑

@carlosfigueira在回复中提供的设置为解决方案的建议提供了良好的结构,但不幸的是,当使用F5运行时,我的机器上会出现错误消息.似乎元数据是必需的,端点也是如此.

Kon*_*ten 8

我终于得到了完全的PO,并开始与商业联系.这就是我制作的 - 它可以在我的机器上运行,我希望它不是一个本地现象.:)

IRestService.cs - 声明,您的代码向联系客户承诺的内容

[ServiceContract]
public interface IRestService
{
  [OperationContract]
  [WebInvoke(Method = "GET", 
    ResponseFormat = WebMessageFormat.Xml, 
    BodyStyle = WebMessageBodyStyle.Wrapped, 
    UriTemplate = "xml/{id}")]
  String XmlData(String id);

  [OperationContract]
  [WebInvoke(Method = "GET", 
    ResponseFormat = WebMessageFormat.Json, 
    BodyStyle = WebMessageBodyStyle.Wrapped, 
    UriTemplate = "json/{id}")]
  String JsonData(String id);
}
Run Code Online (Sandbox Code Playgroud)

RestService.svc.cs - 实现,代码实际对客户端做的事情

public class RestService : IRestService
{
  public String XmlData(String id)
  {
    return "Requested XML of id " + id;
  }

  public String JsonData(String id)
  {
    return "Requested JSON of id " + id;
  }
}
Run Code Online (Sandbox Code Playgroud)

Web.config - 配置,代码在客户端的路上处理的内容

<configuration>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />
  </system.web>

  <system.serviceModel>
    <services>
      ...
    </services>
    <behaviors>
    </behaviors>
  </system.serviceModel>

</configuration>
Run Code Online (Sandbox Code Playgroud)

服务 - 描述服务性质的标签内容

<service name="DemoRest.RestService" 
         behaviorConfiguration="ServiceBehavior">
  <endpoint address="" binding="webHttpBinding" 
            contract="DemoRest.IRestService" 
            behaviorConfiguration="web"></endpoint>
</service>
Run Code Online (Sandbox Code Playgroud)

behavior - 描述服务行为和终点的标记内容

<serviceBehaviors>
  <behavior name="ServiceBehavior">
    <serviceMetadata httpGetEnabled="true"/>
    <serviceDebug includeExceptionDetailInFaults="true"/>
  </behavior>
</serviceBehaviors>

<endpointBehaviors>
  <behavior name="web">
    <webHttp/>
  </behavior>
</endpointBehaviors>
Run Code Online (Sandbox Code Playgroud)

Index.html - 执行程序,您的代码可以被称为

<html>
  <head>
    <script>
      ...
    </script>
    <style>
      ...
    </style>
  </head>
  <body>
    ...
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)

script - 描述JavaScript中可执行文件的标记内容

window.onload = function () {
  document.getElementById("xhr").onclick = function () {
    var xhr = new XMLHttpRequest();
    xhr.onload = function () { alert(xhr.responseText); }
    xhr.open("GET", "RestService.svc/xml/Viltersten");
    xhr.send();
  }
}
Run Code Online (Sandbox Code Playgroud)

style - 描述外观的标记内容

.clickable
{
  text-decoration: underline;
  color: #0000ff;
}
Run Code Online (Sandbox Code Playgroud)

body - 描述标记结构的标记的内容

<ul>
  <li>XML output <a href="RestService.svc/xml/123">
    <span class="clickable">here</span></a></li>
  <li>JSON output <a href="RestService.svc/json/123">
    <span class="clickable">here</span></a></li>
  <li>XHR output <span id="xhr" class="clickable">here</span></li>
Run Code Online (Sandbox Code Playgroud)

一切都存储在一个名为的项目中DemoRest.我创建了自己的文件来声明和实现服务,删除默认的文件.using出于空间原因,省略了指令以及XML版本声明.

现在可以使用以下URL检索响应.

localhost:12345/RestService.svc/xml/Konrad
localhost:12345/RestService.svc/json/Viltersten
Run Code Online (Sandbox Code Playgroud)
  1. 还有其他人让它上班吗?
  2. 有关改进或澄清的任何建议吗?