In the original Visual studio, use a PCL project to join the WCF WebService reference
Using a PCL project in Visual Studio to join the WCF WebService Reference author: Steven Chang 2015/01
Apps often use the WebService service, and in Xamarin, if you want both iOS and Android to be webservice, you can use the PCL project in addition to calling WebService in iOS and Android ( Portable Library class, Chinese is called the Portable category Library, and uses the VisualStudio in the service reference method to quickly establish a WebService service.
Let's say we have a WCF services service, and we have a little bit of modification to the Presets provided by the following program code:
public string GetData (int value)
{
return string. Format ("WebService said:" You entered the value is: {0} ", value);
}
We then set up three projects for Android, iOS, and PCL, and have both Android and iOS refer to the PCL project, such as:
We then add the webservice to the reference using the Join service reference in the PCL project, and if there is a successful service you can see the GetData method established in the previous step, and then add the reference as determined.
At this stage, we have been able to call in the Android or iOS project to join the Service Reference tool to help us build the proxy Class, but we all use the PCL project, of course, can also write the call WebService action in the PCL, Create a category called MyService in the PCL project and set up a GetData method to have its parameters and callback values the same as the GetData on the service, as in the following program code:
Namespace Webservicedemo.service
{
public class MyService
{
public string GetData (int value)
{
}
}
}
In the PCL GetData method, you can start to write the code snippet of the call service, first of all we have to establish a proxy class for service1client, and in general C # use is different, Presets in Xamarin do not support read-only files such as app. config (meaning that system.configuration.* does not exist), so we have to pass in the endpointaddress and binding within the constructor, and define the location of webservice within the EndpointAddress, the following program code:
var binding=new basichttpbinding (Basichttpsecuritymode.none);
var address=new endpointaddress ("Http://testmyws.azurewebsites.net/Service1.svc");
Service1client service = new Service1client (binding, address);
After creating the proxy category, you can call the method provided in the service, and you will find that only asynchronous methods can be called, such as:
Yes, when using the build WebService service in a PCL, only asynchronous methods are available, such as the tail of the call method plus async and the way the tail of the method event name used to inform the result corresponds to the completed, called the event schema asynchronous mode (EAP, The full name is event-based asynchronous Pattern. Do not have to remember ~ know is good), so we have to establish an event in MyService for external calls? No need to be so troublesome, after c#5.0 the async and await keyword, and then derive the work-based Asynchronous Pattern (tap,task-based Asynchronous Pattern), So we can use the TaskCompletionSource class to convert the EAP mode into TAP mode, as in the following code snippet:
var task = new taskcompletionsource<string> ();
Service. getdatacompleted + = (sender, e) = =
{
if (e.cancelled)
Task. Trysetcanceled ();
else if (e.error! = null)
Task. Trysetexception (E.error);
Else
Task. Trysetresult (E.result);
};
Service. Getdataasync (value);
Return task. Task;
When you change to tap mode, you must change the callback value of the method to task:
Public task<string> GetData (int value)
Finally, we take Android as an example, set up the MyService category and call the GetData method, because the GetData callback for the task type, we will use the await keyword, so in the call method also add the Async keyword, as follows:
MyService service = new MyService ();
button. Click +=async (sender,e) =
{
var result =await service. GetData (999);
Toast.maketext (this, result, Toastlength.long). Show ();
};
The results of two platforms were performed by the emulator:
Related Downloads
- Sample file Download
Https://github.com/stevenchang0529/Xamarin.WebServiceDemo
Reference information
- EAP mode
HTTP://MSDN.MICROSOFT.COM/ZH-TW/LIBRARY/WEWWCZDW (v=vs.110). aspx
- Tap mode
http://msdn.microsoft.com/zh-tw/library/hh873175 (v=vs.110). aspx
- EAP to TAP
http://msdn.microsoft.com/zh-tw/library/hh873178 (v=vs.110). aspx#eap
(15) Using a PCL project in Visual Studio to join the WCF WebService reference