有三个地方可以修改SOAP的命名空间:
1、ServiceContract
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; using System.Web.Configuration; namespace WcfTest { // NOTE: You can use the "Rename" command on the "Refactor" menu to change the interface name "IService1" in both code and config file together. [ServiceContract(Name="SoapService",Namespace="http://wcftest.neohope.org")] public interface ISoapService { [OperationContract] [WebInvoke(Method = "GET", UriTemplate = "User/Get/{uid}", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] NUser GetUser(String uid); [OperationContract] [WebInvoke(Method = "POST", UriTemplate = "/User", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] string UpdateUser(NUser newuser); [OperationContract] [WebInvoke(Method = "PUT", UriTemplate = "/User", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] string AddUser(NUser newuser); [OperationContract] [WebInvoke(Method = "DELETE", UriTemplate = "/User", BodyStyle = WebMessageBodyStyle.Bare, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] string DeleteUser(String uid); } [ServiceBehavior(Name = "SoapService", Namespace = "http://wcftest.neohope.org")] [DataContract] public class NUser { string uid = ""; string uname = ""; string usex = ""; [DataMember] public String Uid { get { return uid; } set { uid = value; } } [DataMember] public string UName { get { return uname; } set { uname = value; } } [DataMember] public string USex { get { return usex; } set { usex = value; } } } }
2、服务实现
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.Serialization; using System.ServiceModel; using System.ServiceModel.Web; using System.Text; namespace WcfTest { [ServiceBehavior(Name = "SoapService", Namespace = "http://wcftest.neohope.org")] public class SoapService : ISoapService { static List<NUser> users = new List<NUser>(); static SoapService() { NUser aNUser = new NUser(); aNUser.Uid = "001"; aNUser.UName = "张三"; aNUser.USex = "男"; users.Add(aNUser); } public NUser GetUser(string uid) { foreach (NUser nuser in users) { if (nuser.Uid == uid) { return nuser; } } return null; } public string UpdateUser(NUser newuser) { if (newuser == null) { return "nuewuser is null"; } foreach (NUser nuser in users) { if (nuser.Uid == newuser.Uid) { nuser.UName = newuser.UName; newuser.USex = nuser.USex; return "User updated"; } } return "User not found"; } public string AddUser(NUser newuser) { if (newuser == null) { return "nuewuser is null"; } foreach (NUser nuser in users) { if (nuser.Uid == newuser.Uid) { nuser.UName = newuser.UName; newuser.USex = nuser.USex; return "User updated"; } } users.Add(newuser); return "User added"; } public string DeleteUser(String uid) { bool bFound = false; for(int i=users.Count-1;i>=0;i--) { if (users[i].Uid == uid) { bFound = true; users.RemoveAt(i); } } if (!bFound) { return "User Not Found"; } else { return "User Deleted"; } } } }
3、Web.config
<?xml version="1.0"?> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> </system.web> <system.serviceModel> <services> <service name="WcfTest.SoapService"> <endpoint binding="webHttpBinding" contract="WcfTest.ISoapService" bindingNamespace="http://wcftest.neohope.org"/> </service> </services> <behaviors> <endpointBehaviors> </endpointBehaviors> <serviceBehaviors> <behavior name=""> <serviceMetadata httpGetEnabled="true" /> <serviceDebug includeExceptionDetailInFaults="true" /> </behavior> </serviceBehaviors> </behaviors> <serviceHostingEnvironment aspNetCompatibilityEnabled="false" multipleSiteBindingsEnabled="true" /> </system.serviceModel> <system.webServer> <modules runAllManagedModulesForAllRequests="true"/> <directoryBrowse enabled="true"/> </system.webServer> </configuration>