![](/img/trans.png)
[英]How to pass parameters to windows service via ManagementObject and InvokeMethod()
[英]How to pass parameters to web service via URL
我有可以成功使用的Web服务,但是我正在与要通过URL输入参数的其他人共享Web服务,例如://localhost:12345/Lead.asmx?op=SendFiles&Id=1234678&Name=Joe&Surname=Kevin
我补充说:
<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);
但是我得到一个错误,我必须声明一个主体,因为它没有被标记为抽象,外部或局部。 有人可以帮忙吗?
响应您关于如何创建WCF休息服务的要求...
在您的服务合同中:
[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
然后你会得到
123234
作为您的输出。
如果您仍然需要这里的其余代码。 这是使用控制台应用程序构建的,但可以轻松转换为网络。 真正的进口货是上述的。
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>
当您通过.asmx页面中的测试工具进行测试时,会生成什么URL? 您可以将其提供给呼叫者,并验证他们执行与您执行的相同URL的能力吗?
如果其他使用来自非.NET客户端的服务的人是您的主要用例,则我建议使用基于WCF REST的服务。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.