我创建了一个WCF服务,可以从SQL数据库检索数据,并将数据更新和修改回SQL数据库。我正在尝试从Xamarin for Android和Xamarin for iOS调用WCF方法。我搜索了很多例子,想知道如何通过Xamarin for Android和Xamarin for iOS调用WCF服务的PUT和POST方法,但没有成功。下面是参考的WCF代码...我甚至创建了Web API,但所有消费Web API的示例和教程都是关于如何调用GET方法的。我没有看到任何参考文档,可以展示如何跨平台调用WCF或Web API的PUT或Post方法。我已经通过Fiddler测试了WCF服务并且工作正常。下一步应该怎么做呢?我使用SlsvcUtil.exe创建了这个Web服务的代理,就像Xamarin文档中提到的那样。有人能发一个Xamarin.Android的例子来调用下面WCF服务的更新或删除方法吗?非常需要帮助。服务包含webHttp绑定。
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
>
主要的axml文件
<?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>