WCF4.0新特性-简化配置,WebGet support,Message格式便捷控制,WebFaultException

WCF 的推出整合了.net remoting,webservice 等等,简化了微软平台的通讯开发,可以预计也是微软平台的通讯处理的整合方向。

[简化的配置]

可能都记得,wcf 3.0里面的配置,的确够复杂的;不过这次4.0的发布,微软倒是发布了一些精简的配置办法。

比如我们要Host一个服务,通过下面的简单代码即可,是不是方便了不少?

<?xml version="1.0"?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/>
  </startup>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior>
          <serviceMetadata httpGetEnabled ="true"/>
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <protocolMapping>
      <add binding="wsHttpBinding" scheme ="http"/>
    </protocolMapping>
  </system.serviceModel>
</configuration>
----
    WebServiceHost host = new WebServiceHost(typeof(EchoService), new Uri("http://localhost:8080/EchoService"));
            host.Open();
            Console.WriteLine("Press any key to stop this service.");
            Console.ReadKey();
            host.Close();

[WebGet support]

方便的WebGet支持,只需在ServiceContract 里面做少量的更改即可。
比如:
    [ServiceContract]
    public interface IEcho
    {
        [OperationContract, 
        string Echo(string input);
    }
这样你就可以使用 http://serviceaddress/Echo/inputvalue 的方式来访问了。

[返回Message格式控制]

如果您需要返回的数据是Json 或者 text?
1.定义这样的ServiceContract

[OperationContract]
Message GetUser1(string count);

2.实现服务

  public System.ServiceModel.Channels.Message GetUser1(string count)
        {
            List<UserInfo> result = GetUser(count);
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/json";
            return WebOperationContext.Current.CreateJsonResponse<List<UserInfo>>(result);
        }

这里便返回Json的格式了。当然,还有其他格式的便捷支持.

image

[WebFaultException]
您可以方便的抛出这样的错误,浏览器端将收到 Http 400 bad request的信息。

 throw new WebFaultException<string>("format Error", System.Net.HttpStatusCode.BadRequest);

转载于:https://www.cnblogs.com/ginohuo/archive/2010/05/04/1727105.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值