如何在客户端应用程序中使用wcf服务?

时间:2013-01-09 06:02:11

标签: c# wcf wcf-binding

服务器端代码是,

 [OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "UploadDamagedImage", BodyStyle = WebMessageBodyStyle.Bare, RequestFormat = WebMessageFormat.Json)]        
        NewClsMessage UploadDamagedImage(Details ObjDt123);

客户端代码是,

Details ObjDt = new Details();
            ObjDt.Name = "hi";
            ObjDt.Email = "hi@gmail.com";
            ObjDt.ContactNumber = "3698754215";
            ObjDt.DeviceModel = "E23";
            ObjDt.Problem = "Repair";
            ObjDt.Besttimetocontact = "9am";

在这里,我得到的错误是'对象引用没有设置为对象的实例'。 是否有必要为客户端和服务器端使用相同的对象名称? 如何解决这个问题?

修改

客户端代码是,

Uri uri = new Uri("http://localhost:53865/Service1.svc/UploadDamagedImage");
            Details ObjDt = new Details();
            ObjDt.Name = "hi";
            ObjDt.Email = "hi@gmail.com";
            ObjDt.ContactNumber = "3698754215";
            ObjDt.DeviceModel = "E23";
            ObjDt.Problem = "Repair";
            ObjDt.Besttimetocontact = "9am";

byte[] bytes = File.ReadAllBytes(@"D:\Projects\6mb\1.jpg");
            ObjDt.ImgUpload = bytes;
 DataContractJsonSerializer ser = new DataContractJsonSerializer(typeof(Details));

            MemoryStream mem = new MemoryStream();
            ser.WriteObject(mem, ObjDt);
            string data = Encoding.UTF8.GetString(mem.ToArray(), 0, (int)mem.Length);
            WebClient webClient = new WebClient();
            webClient.UploadStringCompleted += new UploadStringCompletedEventHandler(webClient_UploadStringCompleted);
            webClient.Headers["Content-type"] = "application/json";
            webClient.Encoding = Encoding.UTF8;
            webClient.UploadStringAsync(uri, "POST", data);

服务器端代码是,

public NewClsMessage UploadDamagedImage(Details ObjDt123)
        {
  NewClsMessage ObjNewClsMessage = new NewClsMessage();
            try
            {
 Random rand = new Random();
                int strRandNo = rand.Next();
MemoryStream ms = new MemoryStream(ObjDt.ImgUpload);  
    Image image = Image.FromStream(ms);
}
  catch (Exception ex)
            {

            } 
}

0 个答案:

没有答案