Reputation: 105
I have web service that I can consume successfully, but I am sharing my webservice with someone else who wants to input the parameters via the URL eg: //localhost:12345/Lead.asmx?op=SendFiles&Id=1234678&Name=Joe&Surname=Kevin
I added :
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
to my Web.Config file and my SendFile.asmx.cs code looks like this:
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;
}
I tried adding :
[OperationContract]
[WebGet]
bool PostToDB(string IDNo, string FName, string SName);
But I get an error that I must declare a body because it is not marked abstract, extern or partial. Can anyone help?
Upvotes: 0
Views: 26128
Reputation: 210
In response to your request on how to create a WCF Rest Service...
In your service contract:
[ServiceContract]
public interface ITestService
{
[WebGet(UriTemplate = "Tester")]
[OperationContract]
Stream Tester();
}
On your implementation
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!"));
}
}
This simply prints out all your query string values. You can do whatever processing you'll need to do depending on what query string parameters you get.
For example if you put in.
http://localhost:6666/TestService/Tester?abc=123&bca=234
Then you'll get
123 234
As your output.
Here's the rest of the code if you still need it. this was built using a console app but it can easily be converted to web. The real import stuff are the one's above.
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>
Upvotes: 1
Reputation: 2882
When you test via the test harness in the .asmx page, what URL is generated? Can you give that to your caller and verify their ability to execute the same url you did?
I would recommend a WCF REST based service if others using your service from non .NET clients is your main use case.
Upvotes: 0