c# - Consume WCF service in Xamarin Cross Platform Application -
i have created wcf service retrieves data sql database , can update , modify data sql database. trying call wcf methods xamarin android , xamarin ios. searched alot example how call put , post method wcf service through xamarin android , xamarin ios no luck. have added wcf code below reference. ...even created web api examples , tutorials consume web api how call method . don't see reference document show how call put or post method wcf or web api across cross platform. have tested wcf service through fiddler , worked fine. next step ..i have created proxy web service using slsvcutil.exe mentioned in xamarin documentation. can post 1 example of xamarin.android call update or delete method below wcf service.desperately looking help.service contains webhttp binding.
wcf
service1.svc.cs
using system; using system.collections.generic; using system.linq; using system.runtime.serialization; using system.servicemodel; using system.servicemodel.web; using system.text; public class service1 : iservice1 { public list getdeptslist() { using (deptdbentities entities = new deptdbentities()) { return entities.depts.tolist(); } } public dept getdeptbyid(string no) { try { int deptid = convert.toint32(no); using (deptdbentities entities = new deptdbentities()) { return entities.depts.singleordefault(dept => dept.no == deptid); } } catch { throw new faultexception("something went wrong"); } } public void adddept(string name) { using (deptdbentities entities = new deptdbentities()) { dept dept = new dept { name = name }; entities.depts.add(dept); entities.savechanges(); } } public void updatedept(string no, string name) { try { int deptid = convert.toint32(no); using (deptdbentities entities = new deptdbentities()) { dept dept = entities.depts.singleordefault(b => b.no == deptid); dept.name = name; entities.savechanges(); } } catch(exception e) { throw new faultexception(e.message); } } public void deletedept(string no) { try { int deptid = convert.toint32(no); using (deptappdbentities entities = new deptappdbentities()) { dept dept = entities.depts.singleordefault(b => b.no == deptid); entities.depts.remove(dept); entities.savechanges(); } } catch { throw new faultexception("something went wrong"); } } } web.config
<?xml version="1.0"?> <configuration> <configsections> <section name="entityframework" type="system.data.entity.internal.configfile.entityframeworksection, entityframework, version=4.4.0.0, culture=neutral, publickeytoken=b77a5c561934e089" requirepermission="false"/> </configsections> <system.web> <compilation debug="true" targetframework="4.0"> <assemblies> <add assembly="system.data.entity, version=4.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089"/> </assemblies> </compilation> <pages controlrenderingcompatibilityversion="4.0"/> </system.web> <system.servicemodel> <servicedebug includeexceptiondetailinfaults="false" /> </behavior> </servicebehaviors> </behaviors> <services> <service name="wcfwithjsonp.service1" behaviorconfiguration="restfulbehavior"> <endpoint address="" behaviorconfiguration="webbehavior" binding="webhttpbinding" bindingconfiguration="" contract="wcfwithjsonp.iservice1"/> <host> <baseaddresses> <add baseaddress="http://localhost/service1"/> </baseaddresses> </host> <endpoint address="mex" binding="mexhttpbinding" contract="imetadataexchange"/> </service> </services> <behaviors> <endpointbehaviors> <behavior name="webbehavior"> <webhttp defaultoutgoingresponseformat="json"/> </behavior> </endpointbehaviors> <servicebehaviors> <behavior name="restfulbehavior"> <servicemetadata httpgetenabled="true"/> <servicedebug includeexceptiondetailinfaults="false"/> </behavior> </servicebehaviors> </behaviors> <servicehostingenvironment multiplesitebindingsenabled="true"/> </system.servicemodel> <system.webserver> <modules runallmanagedmodulesforallrequests="true"/> <directorybrowse enabled="true"/> </system.webserver> <entityframework> <defaultconnectionfactory type="system.data.entity.infrastructure.localdbconnectionfactory, entityframework"> <parameters> <parameter value="v12.0"/> </parameters> </defaultconnectionfactory> </entityframework> </configuration >
main.axml for
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:minwidth="25px" android:minheight="25px"> <linearlayout android:orientation="vertical" android:layout_width="134.1dp" android:layout_height="fill_parent" android:minwidth="25px" android:minheight="25px"> <textview android:text="enter no:" android:textappearance="?android:attr/textappearancesmall" android:layout_width="163.4dp" android:layout_height="wrap_content" android:id="@+id/no" android:layout_marginbottom="27.5dp" android:layout_margintop="0.0dp" android:layout_marginleft="5dp" /> <textview android:text="enter name:" android:textappearance="?android:attr/textappearancesmall" android:layout_width="252.7dp" android:layout_height="wrap_content" android:id="@+id/name" android:layout_marginbottom="27.5dp" android:layout_margintop="0.0dp" android:layout_marginleft="5dp" android:enabled="false" android:visibility="invisible" /> </linearlayout> <button android:id="@+id/get" android:layout_width="fill_parent" android:layout_height="36.6dp" android:text="get" /> <button android:id="@+id/add" android:layout_width="fill_parent" android:layout_height="36.6dp" android:text="add" /> <button android:id="@+id/update" android:layout_width="fill_parent" android:layout_height="36.6dp" android:text="update" /> <button android:id="@+id/delete" android:layout_width="fill_parent" android:layout_height="36.6dp" android:text="delete" /> <textview android:text="" android:textappearance="?android:attr/textappearancesmall" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/valueno" android:layout_marginbottom="27.5dp" android:layout_margintop="0.0dp" android:background="@android:color/holo_purple" /> <textview android:text="" android:textappearance="?android:attr/textappearancesmall" android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/valuename" android:layout_marginbottom="27.5dp" android:layout_margintop="0.0dp" android:background="@android:color/holo_purple" /> </linearlayout>
here simple code snippet use guide:
//first @ class level, create private variable client. private service1client _client; private button _addbuton; private textview _txtdeptname; //initialize _client in oncreate() method. protected override void oncreate(bundle bundle) { base.oncreate(bundle); var endpoint = new endpointaddress("http://<ipaddress:port>/service1.svc"); var binding = new basichttpbinding{ name = "basichttpbinding", maxbuffersize = 2147483647, maxreceivedmessagesize = 2147483647 }; timespan timeout = new timespan(0, 0, 30); binding.sendtimeout = timeout; binding.opentimeout = timeout; binding.receivetimeout = timeout; _client = new service1client(binding, endpoint); _client.adddeptcompleted += clientadddeptcompleted; _addbutton = findviewbyid<button>(android.resources.id.add); _addbutton.click += addbutton_clicked; _txtdeptname = findviewbyid<textview>(android.resources.id.name); } //then within event handlers, public void addbutton_clicked(object sender, eventargs e) { _client.adddeptasync(_txtdeptname.text); } //handle request completed event. private void clientadddeptcompleted(object sender, adddeptcompletedeventargs adddeptcompletedeventargs) { //todo: notification request has completed. } you should able follow similar pattern other buttons , service calls well. apologize if have made typos. i'm going bit of memory , of wcf instructions on xamarin site.
Comments
Post a Comment