如何在windows phone7应用程序中使用wcf服务?

时间:2013-01-09 12:55:42

标签: c# wcf windows-phone-7 wcf-binding wcf-security

我在服务器端使用了这段代码,

[OperationContract]        
        [WebInvoke(Method = "POST", UriTemplate = "/UploadData?Name={Name}&Email={Email}&ContactNumber={ContactNumber}&DeviceModel={DeviceModel}&Problem={Problem}&Besttimetocontact={Besttimetocontact}",        
            BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Xml, RequestFormat = WebMessageFormat.Xml)]
        NewClsMessage UploadData(string Name, string Email, string ContactNumber, string DeviceModel, string Problem, string Besttimetocontact);

 public NewClsMessage UploadData(string Name, string Email, string ContactNumber, string DeviceModel, string Problem, string Besttimetocontact)
{
            NewClsMessage ObjNewClsMessage = new NewClsMessage();
            try
            {
  str_value = "insert into Estimate(Name,Email,ContactNumber,DeviceModel,Problem,Besttimetocontact) values('" + Name + "','" + Email + "','" + ContactNumber + "','" + DeviceModel + "','" + Problem + "', '" + Besttimetocontact + "')";                 

                OpenConnection();
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.CommandText = str_value;
                value = sqlCmd.ExecuteNonQuery();
                if (value > 0)
                {
                    ObjNewClsMessage = new NewClsMessage();
                    ObjNewClsMessage.Result = "1";
                    ObjNewClsMessage.Message = "send Successfully";
                }
                else
                {
                    ObjNewClsMessage = new NewClsMessage();
                    ObjNewClsMessage.Result = "0";
                    ObjNewClsMessage.Message = "Free Estimate not send Successfully";
                }
}

 catch (Exception ex)
            {
            }
            return ObjNewClsMessage;

客户端代码是,

WebClient webClient = new WebClient();
            webClient.Headers[HttpRequestHeader.ContentType] = "application/xml";
            //  var uri = new Uri("localhost:55666/WcfPostMethod/Service.svc/UploadXml");
            var uri = new Uri("http://localhost:50546/Service1.svc/UploadData");
            StringBuilder postData = new StringBuilder();
            postData.AppendFormat("?{0}={1}", "Name", HttpUtility.UrlEncode("hi"));
            postData.AppendFormat("&{0}={1}", "Email", HttpUtility.UrlEncode("hi@gmail.com"));
            postData.AppendFormat("&{0}={1}", "ContactNumber", HttpUtility.UrlEncode("56898"));
            postData.AppendFormat("&{0}={1}", "DeviceModel", HttpUtility.UrlEncode("E4376"));
            postData.AppendFormat("&{0}={1}", "Problem", HttpUtility.UrlEncode("Repair"));
            postData.AppendFormat("&{0}={1}", "Besttimetocontact", HttpUtility.UrlEncode("9am"));            
            webClient.Headers[HttpRequestHeader.ContentLength] = postData.Length.ToString();
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
            webClient.UploadStringAsync(uri, "POST", postData.ToString());

在这里,我收到服务器端的错误..在客户端应用程序中发布数据意味着,在服务器端,数据变空(null)值。如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

您能告诉我们您的wcf服务是否可以在没有您的客户的情况下运行吗?

这是一个很好的工具(Storm或WCF Storm)

Storm

您甚至可以找到样本:

Consume WCF service with Windows Phone 7 Application