码迷,mamicode.com
首页 > 其他好文 > 详细

调用WCF不需要添加服务引用,使用一个WCFHelper类就可以

时间:2014-07-25 13:55:51      阅读:2515      评论:0      收藏:0      [点我收藏+]

标签:des   style   blog   http   java   color   使用   os   

效果图:

 

bubuko.com,布布扣

 

 

调用过程:

 string WCFURL = "http://localhost:100/Service1.svc";

            UserRequest user = new UserRequest { UserName = UserName.Text, Address = Address.Text, Email = Email.Text };

            string request = "{\"request\":" + new JavaScriptSerializer().Serialize(user) + "}";
            string returnContent = WCFHelper.SendHttpRequest(WCFURL, "GetUserInfo", request);

            UserResult re = new JavaScriptSerializer().Deserialize<UserResult>(returnContent);

            lblText.Text = re.Result;

 

WCF项目中配置文件节点配置: 

bubuko.com,布布扣
 1 <system.serviceModel>
 2     <behaviors>
 3       <endpointBehaviors>
 4         <behavior name="httpBehavior">
 5           <webHttp />
 6         </behavior>
 7       </endpointBehaviors>
 8       <serviceBehaviors>
 9         <behavior name="">
10           <serviceMetadata httpGetEnabled="true" />
11           <serviceDebug includeExceptionDetailInFaults="false" />
12         </behavior>
13       </serviceBehaviors>
14     </behaviors>
15     <services>
16       <service name="WCFServices.Service1">
17         <endpoint address=""
18                   behaviorConfiguration="httpBehavior"
19                   binding="webHttpBinding"
20                   contract="WCFServices.IService1" />
21       </service>
22     </services>
23   </system.serviceModel>
View Code

 

 

WCF项目请求接口:

bubuko.com,布布扣
 [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "GetUserInfo",
            BodyStyle = WebMessageBodyStyle.WrappedRequest, //包装请求,但不包装相应  注:如果设置为包装相应,返回的JSON结果会加一个 壳
            ResponseFormat = WebMessageFormat.Json,
            RequestFormat = WebMessageFormat.Json)]
        UserResult GetUserInfo(UserRequest request);
    }
View Code

 

 

WCF项目实现方法:

bubuko.com,布布扣
  public UserResult GetUserInfo(Model.UserRequest request)
        {
            return new UserResult { Result = request.UserName + "地址是:" + request.Address + ",邮箱是:" + request.Email };
        }
View Code

 

 

项目下载地址: http://download.csdn.net/detail/vincent_void/7676403

调用WCF不需要添加服务引用,使用一个WCFHelper类就可以,布布扣,bubuko.com

调用WCF不需要添加服务引用,使用一个WCFHelper类就可以

标签:des   style   blog   http   java   color   使用   os   

原文地址:http://www.cnblogs.com/vincentvoid/p/3867647.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!