如何使用WCF服务生成XML输出? [英] How to produce XML output using WCF service?

查看:282
本文介绍了如何使用WCF服务生成XML输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经设置了如下界面。

  [的ServiceContract] 
公共接口IService1
{
[OperationContract的]
串平();
}



它的实现方式如下:

 公共类服务:IService1 
{
公共字符串平(){返回傍; }
}



据VS测试应用程序调用时,它的正常工作。我的问题是,我想出现在屏幕上的文字,当我键入的的http://本地主机:12345 / Service1.svc 的(或可能的 Service1.svc平安 Service.svc /平的)。它是完全关闭还是我吠叫右树?



当然,的最终会是一个XML strucure。



修改



由@carlosfigueira在答复中提出的设立列出了一良好的结构,以寻求解决方案的建议但不幸的是使用F5键运行时,我的机器上导致错误消息。看来,元数据是必需的,而且同样适用于端点。


解决方案

我终于完全PO和去了业务全联系。这就是我所生产的 - 它的作品在我的机器上,我希望它不是一个局部现象。 :)



IRestService.cs - 报关,你的代码中承诺一个接触客户

  [的ServiceContract] 
公共接口IRestService
{
[OperationContract的]
[WebInvoke(方法=GET,
ResponseFormat = WebMessageFormat.Xml,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate =XML /(编号))
字符串XMLDATA(字符串ID);

[OperationContract的]
[WebInvoke(方法=GET,
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = JSON /(编号))
字符串JsonData(字符串ID);
}



RestService.svc.cs - 的实施,你的代码实际上确实给客户

 公共类RestService:IRestService 
{
公共字符串XMLDATA(字符串ID)
{
返回请求ID的XML+ ID;
}

公共字符串JsonData(字符串ID)
{
返回请求ID的JSON+ ID;
}
}



的Web.config - 配置方面,你的代码作为的方式处理,客户端



<预类=郎咸平的XML prettyprint-覆盖> <结构>

<&的System.Web GT;
<编译调试=真targetFramework =4.0/>
< /system.web>

< system.serviceModel>
<服务和GT;

< /服务>
<&行为GT;
< /行为>
< /system.serviceModel>

< /结构>

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



<预类=郎咸平的XML prettyprint-覆盖> <服务名称=DemoRest.RestService
behaviorConfiguration =ServiceBehavior>
<端点地址=绑定=的WebHttpBinding
合同=DemoRest.IRestService
behaviorConfiguration =网络>< /端点>
< /服务>

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



<预类=郎咸平的XML prettyprint-覆盖> < serviceBehaviors>
<行为NAME =ServiceBehavior>
< serviceMetadata httpGetEnabled =真/>
< serviceDebug includeExceptionDetailInFaults =真/>
< /行为>
< / serviceBehaviors>

< endpointBehaviors>
<行为NAME =网络>
< webHttp />
< /行为>
< / endpointBehaviors>



的index.html - 执行者,你的代码可以称为



<预类=郎HTML prettyprint-覆盖> < HTML和GT;
< HEAD>
<脚本>

< / SCRIPT>
<风格>

< /风格>
< /头>
<身体GT;

< /身体GT;
< / HTML>

脚本的 - 描述JavaScript中的可执行文件标记的内容。



<预类=郎咸平的JavaScript prettyprint-覆盖> 的window.onload =函数(){
的document.getElementById(XHR)。的onclick =函数(){
变种XHR =新的XMLHttpRequest();
xhr.onload =函数(){警报(xhr.responseText); }
xhr.open(GET,RestService.svc / XML / Viltersten);
xhr.send();
}
}

风格的 - 内容描述外观



<预类=郎CSS prettyprint-覆盖> .clickable
{
文本修饰标记:下划线;
颜色:#0000FF;
}

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



<预类=郎HTML prettyprint-覆盖> < UL>
<立GT; XML输出< A HREF =RestService.svc / XML / 123>
<跨度类=点击>此处< / SPAN>< / A>< /李>
<立GT; JSON输出< A HREF =RestService.svc / JSON / 123>
<跨度类=点击>此处< / SPAN>< / A>< /李>
<立GT; XHR输出<跨度ID =XHR级=点击>此处< / SPAN>< /李>





一切都存储在项目名为 DemoRest 。我创建了自己的文件申报和实施服务,删除默认的。的使用以及XML版本声明中的指令省略空间的原因。



现在的反应可以。使用以下网址检索

 本地主机:12345 / RestService.svc / XML /康拉德
本地主机:12345 / RestService .SVC / JSON / Viltersten




  1. 有没有人得到它的工作太?

  2. 在改善或澄清?


任何建议

I've set up the following interface.

[ServiceContract]
public interface IService1
{
  [OperationContract]
  String Ping();
}

Its implementation is as follows.

public class Service1 : IService1
{
  public string Ping(){ return "Pong"; }
}

According to the testing application in VS it's working properly when invoked. My problem is that I'd like the text to appear on the screen when I type http://localhost:12345/Service1.svc (or maybe Service1.svc?Ping or Service.svc/Ping). Is it totally off or am I barking up the right tree?

Of course, "Pong" will eventually be an XML strucure.

EDIT

The set-up presented in the reply by @carlosfigueira below gives a good structure to a suggestion for a solution but unfortunately leads on my machine to an error message when run using F5. It seems that metadata is required and that the same goes for endpoints.

解决方案

I finally got totally PO and went off to business full contact. This is what I've produced - it works on my machine and I hope it's not a local phenomenon. :)

IRestService.cs - the declaration, what your code promises to a contacting client

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

RestService.svc.cs - the implementation, what your code actually does to the client

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

Web.config - the configuration, what your code is handled as on the way to the client

<configuration>

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

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

</configuration>

services - contents of the tag describing the service's nature

<service name="DemoRest.RestService" 
         behaviorConfiguration="ServiceBehavior">
  <endpoint address="" binding="webHttpBinding" 
            contract="DemoRest.IRestService" 
            behaviorConfiguration="web"></endpoint>
</service>

behaviors - contents of the tag describing the behavior of the service and the end-point

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

<endpointBehaviors>
  <behavior name="web">
    <webHttp/>
  </behavior>
</endpointBehaviors>

Index.html - the executor, what your code can be called as

<html>
  <head>
    <script>
      ...
    </script>
    <style>
      ...
    </style>
  </head>
  <body>
    ...
  </body>
</html>

script - contents of the tag describing the executable in 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();
  }
}

style - contents of the tag describing the appearance

.clickable
{
  text-decoration: underline;
  color: #0000ff;
}

body - contents of the tag describing the markup structure

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

Everything is stored in a project called DemoRest. I created my own files for declaration and implementation of the service, removing the default ones. The directives of using as well as the XML version declaration are omitted for spacial reasons.

Now the response can be retrieved using the following URL.

localhost:12345/RestService.svc/xml/Konrad
localhost:12345/RestService.svc/json/Viltersten

  1. Does anybody else get it to work too?
  2. Any suggestions on improvement or clarification?

这篇关于如何使用WCF服务生成XML输出?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆