2012-10-24 188 views
0

我有我可以成功使用的Web服務,但我與其他想通過URL輸入參數的其他人共享我的web服務,例如:// localhost:12345/Lead。 ?ASMX OP = SendFiles &編號= 1234678 &名稱=喬&姓=凱文如何通過URL將參數傳遞到Web服務

我說:

<webServices> 
     <protocols> 
     <add name="HttpGet"/> 
     </protocols> 
    </webServices> 

到我的web.config文件,我SendFile.asmx.cs代碼如下所示:

namespace SendFiles 
    { 
     /// <summary> 
     /// Summary description for Service1 
     /// </summary> 
    [WebService(Namespace = "http://testco.co.za/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService] 
    public class SendFile : System.Web.Services.WebService 
    { 

     [WebMethod] 
     public bool PostToDB(LoadEntity _lead) 
     { 

      ConnectToSQLDB(ConfigurationManager.AppSettings["Server"], ConfigurationManager.AppSettings["DB"], 
           ConfigurationManager.AppSettings["UserName"], ConfigurationManager.AppSettings["Password"], ref connectionRef); 

      if (LI.ImportFiles(_lead, ref (error)) == true) 
      { 
       return true; 
      } 
      else 
       return false; 
     } 

我嘗試添加:

[OperationContract] 
    [WebGet] 
    bool PostToDB(string IDNo, string FName, string SName); 

但我得到的,因爲它沒有標記爲抽象,EXTERN或部分,我必須聲明主體錯誤。誰能幫忙?

+0

您是否嘗試過將該方法標記爲Public或Private或Static例如..? – MethodMan

回答

1

在回答關於如何創建一個WCF REST服務,您的請求......

在服務合同:

[ServiceContract] 
public interface ITestService 
{ 
    [WebGet(UriTemplate = "Tester")] 
    [OperationContract] 
    Stream Tester(); 
} 

您的實現

public class TestService : ITestService 
{ 
    public Stream Tester() 
    { 
     NameValueCollection queryStringCol = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters; 

     if (queryStringCol != null && queryStringCol.Count > 0) 
     { 
      string parameters = string.Empty; 
      for (int i = 0; i < queryStringCol.Count; i++) 
      { 
       parameters += queryStringCol[i] + "\n"; 
      } 

      return new MemoryStream(Encoding.UTF8.GetBytes(parameters)); 
     } 
     else 
      return new MemoryStream(Encoding.UTF8.GetBytes("Hello Jersey!")); 
    } 
} 

這只是打印出所有查詢字符串值。根據您獲得的查詢字符串參數,您可以執行任何需要執行的處理。

例如,如果你把。

http://localhost:6666/TestService/Tester?abc=123&bca=234 

然後你會得到

作爲輸出。

如果你仍然需要它,下面是其餘的代碼。這是使用控制檯應用程序構建的,但可以輕鬆轉換爲網頁。真正的進口東西是上面的東西。

class Program 
{ 
    static ServiceHost _service = null; 

    static void Main(string[] args) 
    { 
     _service = new ServiceHost(typeof(TestService)); 
     _service.Open(); 

     System.Console.WriteLine("TestService Started..."); 
     System.Console.WriteLine("Press ENTER to close service."); 
     System.Console.ReadLine(); 

     _service.Close(); 
    } 
} 

<configuration> 
<startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0"/> 
    </startup> 
    <system.serviceModel> 
    <serviceHostingEnvironment aspNetCompatibilityEnabled="true"/> 
    <services> 
     <service name="ConsoleApplication1.TestService"> 
     <host> 
      <baseAddresses> 
      <add baseAddress="http://localhost:6666/TestService"/> 
      </baseAddresses> 
     </host> 
     <endpoint binding="webHttpBinding" contract="ConsoleApplication1.ITestService" 
      behaviorConfiguration="webHttp"/> 
     </service>  
    </services> 
    <bindings> 
     <webHttpBinding> 
     <binding name="webHttpBinding" maxReceivedMessageSize="2147483647" maxBufferSize="2147483647"> 
      <readerQuotas maxArrayLength="2147483647" maxStringContentLength="2147483647"/> 
     </binding> 
     </webHttpBinding> 
    </bindings> 
    <behaviors> 
     <serviceBehaviors> 
     <behavior>   
      <serviceDebug includeExceptionDetailInFaults="false"/> 
     </behavior> 
     </serviceBehaviors> 
     <endpointBehaviors> 
     <behavior name="webHttp"> 
      <webHttp/> 
     </behavior> 
     </endpointBehaviors> 
    </behaviors> 
    </system.serviceModel> 
</configuration> 
0

當您通過.asmx頁面中的測試工具測試時,會生成什麼URL?你可以給你的調用者,並驗證他們執行相同的網址的能力嗎?

如果其他人使用您的來自非.NET客戶端的服務是您的主要用例,那麼我會推薦一個基於WCF REST的服務。

+0

我對此很新。請你能給我一個關於如何使用WCF REST服務的例子嗎? – user1668123

+0

@ user1668123 http://stackoverflow.com/a/12901839/932418 –

+0

你可以從這裏開始http://visualstudiogallery.msdn.microsoft.com/fbc7e5c1-a0d2-41bd-9d7b-e54c845394cd假設你正在使用Visual Studio 2010。如果不是,請查看WebInvoke屬性http://msdn.microsoft.com/en-us/library/system.servicemodel.web.webinvokeattribute.aspx。您需要在RouteTable中註冊您的服務類作爲Global.asax.cs中Application_Start的一部分 – mckeejm