How to change WebService code behind class dynamically? - c#

I'm facing an issue with different versions of a Web Service.
Because there are several versions of the Web service, sometimes the parameters are changed and/or WebMethods are added and removed.
I want to have a single asmx file, but depending on the client installation (the version they are running), be able to change the code behind of the asmx at runtime.
Instead of having different asmx per version, just have one asmx file that dynamically can load the code behind with the accurate version. In this case I do have a V1Methods.cs, V2Methods.cs, V10Methods.cs
<%# WebService Language="C#" Class="DynamicClass" %>
If the customer is running Version2, the asmx code behind class should be V2Methods.cs and so on.
Is it possible?

In short no, that is not possible. I was going to suggest using the webservice as a facade but by the sounds of it the method signatures on each version are different, which would make that more difficult.
If the client application is dependent on a particular version of your webservice, can't you just deploy all versions of your service with different names (i.e. servicev1.asmx, servicev2.asmx etc), and add some config to your client to tell it which one to call ?

OK - I have a possible solution for you that is not award-winning for elegance but I've just tested it and it works.
You can expose one WebMethod that returns object and takes a params object[] parameter, allowing you to pass whatever you like to it (or nothing) and return whatever you want. This compiles to legal WSDL using the 'anyType' type.
If you can identify which actual method to call based on the number and datatype of parameters passed to this method, you can call the appropriate method and return whatever value you want.
The service: -
[WebMethod]
public object Method(params object[] parameters)
{
object returnValue = null;
if (parameters != null && parameters.Length != 0)
{
if (parameters[0].GetType() == typeof(string) && parameters[1].GetType() == typeof(int))
{
return new ServiceImplementation().StringIntMethod(parameters[0].ToString(), Convert.ToInt32(parameters[1]));
}
else if (parameters[0].GetType() == typeof(string) && parameters[1].GetType() == typeof(string))
{
return new ServiceImplementation2().StringStringMethod(parameters[0].ToString(), parameters[1].ToString());
}
}
return returnValue;
}
My test service implementation classes: -
public class ServiceImplementation
{
public string StringIntMethod(string someString, int someInt)
{
return "StringIntMethod called";
}
}
public class ServiceImplementation2
{
public float StringStringMethod(string someString, string someOtherString)
{
return 3.14159265F;
}
}
An example of use: -
var service = new MyTestThing.MyService.WebService1();
object test1 = service.Method(new object[] { "hello", 3 });
Console.WriteLine(test1.ToString());
object test2 = service.Method(new object[] { "hello", "there" });
Console.WriteLine(test2.ToString());
I've tested this and it works. If you're interested, the WSDL that "Method" generates: -
POST /test/WebService1.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/Method"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<Method xmlns="http://tempuri.org/">
<parameters>
<anyType />
<anyType />
</parameters>
</Method>
</soap:Body>
</soap:Envelope>
Incase you're wondering, yes I am bored at work and I'm in the mood for helping people :)

Related

Twilio Gather not pausing for input

I'm having trouble getting Twilio to perform a Gather. The call initializes just fine but instead of waiting for a user keypress, the Gather just falls through to the next statement and hangs up.
My environment is Visual Studio 2015. .NET 4.6, MVC6, asp.net5. I have the RC1 Update installed. Nuget packages are Twilio version 4.4.1, Twilio.TwiML 3.3.6.
Here is a test WebAPI 2 controller:
[Route("api/[controller]")]
public class OutboundCallController : Controller
{
[HttpPost]
public IActionResult Post()
{
var twilioResponse = new TwilioResponse();
twilioResponse.BeginGather(new { timeout = "60", numDigits = "1", action = "Foo", method = "POST" });
twilioResponse.Say("Test Message Here");
twilioResponse.EndGather();
twilioResponse.Say("Fallthrough. Goodbye.");
return new ObjectResult(twilioResponse.ToString());
}
}
When Twilio receives the below data it Says "Test Message Here Fallthrough. Goodbye." all at once, without pausing, then promptly hangs up.
Using ngrok I can see that the reponse to the Twilio POST to my controller is:
<Response>
<Gather timeout="60" numDigits="1" action="Foo" method="POST">
<Say>Test Message Here</Say>
</Gather>
<Say>Fallthrough. Goodbye.</Say>
</Response>
Additionally, my Twilio log looks like (identical):
<Response>
<Gather timeout="60" numDigits="1" action="Foo" method="POST">
<Say>Test Message Here</Say>
</Gather>
<Say>Fallthrough. Goodbye.</Say>
</Response>
EDIT:
I've also tried changing the WebAPI to return a string instead of IActionResult. Nothing changes, same result.
[HttpPost]
public string Post()
{....}
ANSWER:
Turns out I wasn't returning the correct content type, I modified the return of the POST action, the full controller code is below:
[Route("api/[controller]")]
public class OutboundCallController : Controller
{
[HttpPost]
public IActionResult Post()
{
var twilioResponse = new TwilioResponse();
twilioResponse.BeginGather(new { timeout = "60", numDigits = "1", action = "Foo", method = "POST" });
twilioResponse.Say("Test Message Here");
twilioResponse.EndGather();
twilioResponse.Say("Fallthrough. Goodbye.");
return Content(twilioResponse.ToString(), "application/xml");
}
}
Twilio developer evangelist here.
I just copied your generated TwiML to twimlbin and was able to get it working with proper pause and without skipping.
Here's an exact copy of your generated TwiML.
Obviously when you press a number, it fails because the action is only set to be foo. If you set that action to something else like the example below, you will see that upon pressing a number, you should also get a message that says "Hi there"
<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Gather timeout="60" numDigits="1" action="http://twimlets.com/echo?Twiml=%3CResponse%3E%3CSay%3EHi+there.%3C%2FSay%3E%3C%2FResponse%3E" method="POST">
<Say>Test Message Here</Say>
</Gather>
<Say>Fallthrough. Goodbye.</Say>
</Response>
Also, your C# code looks right but let me know you want to make your endpoint public so I can test it. It will be worth checking that what you're returning to Twilio is really XML (i.e. that it has <?xml version="1.0" encoding="UTF-8"?> on top) and that its content type is really XML.
Happy to help with any other questions.

How to change WebMethod XML output for ASP.NET Webservice, specifically namespace declaration?

I'm developing an ASP.NET Webservice (not WCF) for a given client. This is one of those situations, where you can not change anything at the client.
The client sends the following XML to request a method:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:cometxsd="http://werk-ii.de/soap/comet/Schema"
xmlns:comet="http://werk-ii.de/soap/comet"
xmlns:xop="http://www.w3.org/2004/08/xop/include"
xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing"
xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"
xmlns:xmime5="http://www.w3.org/2005/05/xmlmime"
xmlns:ns1="http://soap.comet.werkii.com/">
<SOAP-ENV:Body>
<ns1:login xsi:type="ns1:login">
<user>myusername</user>
<password>mypassword</password>
<client>whatever</client>
<language>de</language>
</ns1:login>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
My Service provides the login-Method like this:
[WebService(Namespace = "http://soap.comet.werkii.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class CometService : WebService
{
[WebMethod(MessageName = "login")]
[return: XmlElement("return")]
public LoginResult Login (string user, string password, string client, string language)
{
return new LoginResult() {
ResultCode = 0,
SessionId = user + "-" + password + "-" + client + "-" + language
};
}
}
public class LoginResult
{
[XmlElement("resultCode")]
public int ResultCode { get; set; }
[XmlElement("sessionId")]
public string SessionId { get; set; }
}
If I start the service, it tells me what SOAP 1.1 code I have to send as a request, that ist:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<login xmlns="http://soap.comet.werkii.com/">
<user>string</user>
<password>string</password>
<client>string</client>
<language>string</language>
</login>
</soap:Body>
</soap:Envelope>
When I use this code - as told by the service - from another test client (i wrote one in PHP), everything works fine an I get a result. But when I send the code from the beginning (which is what the real client will send), the method is called but all 4 arguments are null.
From an XML view, in my opinion both requests are the same. The only difference is, where the namespace is defined and if elements use the ns1 prefix. This should not make any difference, when the service reads it as XML. Maybe I'm wrong.
Perhaps the 4 arguments in the first XML have a different namespace (none) than the method (ns1). Can that be the reason why all arguments are null? How would I change the namespace for the arguments only?
When I change only the method lines in XML - replacing <ns1:login xsi:type="ns1:login"> with <login xmlns="http://soap.comet.werkii.com/"> and also the closing tag - it works! So the service seems not to understand my request, if the method element uses a namespace prefix, though the namespace is properly defined in the root element.
I tried the following to change the XML format which the service expects:
System.Web.Services.Protocols.SoapDocumentMethodAttribute - no effect at all
XmlNamespaceDeclarationsAttribute as shown here - which seems not to work because it is made for manipulating complex types, not the service class or a method
So the question is, how can I tell my service to accept the XML from the first example?
Good to know that parameters can also have Attributes:
public LoginResult Login (
[XmlElement(Namespace = "")] string user,
[XmlElement(Namespace = "")] string password,
[XmlElement(Namespace = "")] string client,
[XmlElement(Namespace = "")] string language)
{
return new LoginResult() {
ResultCode = 0,
SessionId = user + "-" + password + "-" + client + "-" + language
};
}
That's the solution to put the parameters into the global namespace – problem solved.

Can't pass java parameters in the same format as c# for WCF service

This is the scenario: I have a WCF service running, who communicates with this method, in C#:
public bool ValidateUser(UserPass up)
{
initializeAttributes();
IMembershipService Member = new AccountMembershipService();
bool login = Member.ValidateUser(up.User, up.Pass);
return login;
}
The parameter are encapsulated in this class:
[DataContract]
public class UserPass
{
string user = "";
string pass = "";
string email = "";
[DataMember]
public string User
{
get { return user; }
set { user = value; }
}
[DataMember]
public string Pass
{
get { return pass; }
set { pass = value; }
}
[DataMember]
public string Email
{
get { return email; }
set { email = value; }
}
}
Now, I want to connect to the server via an Android application, now, my question is, how can I replicate the UserPass class in Java, so the ValidateUser method can receive its parameter in a way it can understands it.
for reference, this is the code where I'm obtaining the User and Password:
private void validateUser(String user, String pass)
{
String SOAP_ACTION = "http://tempuri.org/IUserService/ValidateUser/";
String METHOD_NAME = "ValidateUser";
String NAMESPACE = "http://tempuri.org/";
String URL = "http://10.0.2.2/UserService.svc";
AlertDialog popup;
SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
request.addProperty(user, pass);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
envelope.dotNet = true;
HttpTransportSE httpTransport = new HttpTransportSE(URL);
try
{
httpTransport.call(SOAP_ACTION, envelope); //here's the exception!!
Object response = envelope.getResponse();
popup = createAlertDialog("Respuesta",response.toString(),"OK");
popup.show();
}
catch (Exception exception)
{
String exceptionStr=exception.toString();
popup = createAlertDialog("Exception!!",exceptionStr,"OK");
popup.show();
}
}
The exception it throws is xmlpullparserexception, which, according to my understanding, is because of a missmatch between the parameters of the request and the actual method.
Many thanks for reading my question, and many more for those who can answer it :)
EDIT:
I finnaly got how to compare the XMLs... now, this is what my SOAP is providing:
<v:Envelope xmlns:i="http://www.w3.org/2001/XMLSchema-instance"
xmlns:d="http://www.w3.org/2001/XMLSchema"
xmlns:c="http://schemas.xmlsoap.org/soap/encoding/"
xmlns:v="http://schemas.xmlsoap.org/soap/envelope/">
<v:Header />
<v:Body>
<ValidateUser xmlns="http://tempuri.org/" id="o0" c:root="1">
<User i:type="d:string">someuser</User>
<Pass i:type="d:string">somepass</Pass>
<Email i:type="d:string"></Email>
</ValidateUser>
</v:Body>
and this is what it SHOULD have made (retrieved from WCF Test Client application from Visual Studio 2010):
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
<s:Header>
<Action s:mustUnderstand="1" xmlns="http://schemas.microsoft.com/ws/2005/05/addressing/none">http://tempuri.org/IUserService/ValidateUser</Action>
</s:Header>
<s:Body>
<ValidateUser xmlns="http://tempuri.org/">
<up xmlns:d4p1="http://schemas.datacontract.org/2004/07/LiveAndesWCF" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<d4p1:Email i:nil="true" />
<d4p1:Pass>somepass</d4p1:Pass>
<d4p1:User>someuser</d4p1:User>
</up>
</ValidateUser>
</s:Body>
</s:Envelope>
Now, I'm lost on how to code my soap code to have it generate a xml file like the latter one.
Many thanks again.
have you tried to look at the xml created by the soap call? you can compare it to the xml created by a .net proxy. maybe this helps to find a solution.
here is how you can enable the logging of the soap calls:
http://msdn.microsoft.com/en-us/library/ms730064.aspx
This line looks suspect to me:
request.addProperty(user, pass);
As far as I can tell, SoapObject comes from the KSOAP2 library, and according to the docs, addProperty takes the name of the property and the value. To set user and pass, I would expect something more like this:
request.addProperty("user", user);
request.addProperty("pass", pass);
Currently, it looks like you're adding a single property named using the value of the user parameter. If the endpoint is expecting at least 2 arguments, then this could be the source of your mismatch.
Also, is the value "Email", from the UserPass wrapper class, optional? As I don't see it being set anywhere, and the wrapper class suggests it's required by the SOAP request

C# + XML Serialization

I have a method that is calling a web service. When this web service is called, the following method is called:
[System.Web.Services.Protocols.SoapDocumentMethodAttribute(
"http://mydomain.com/services/DoSomething",
RequestNamespace = "http://mydomain.com/services",
ResponseNamespace = "http://mydomain.com/services",
Use = System.Web.Services.Description.SoapBindingUse.Literal,
ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
[return: System.Xml.Serialization.XmlElementAttribute("MyResponse")]
public MyResponse DoSomethingr(MyRequest myRequest)
{
object[] results = this.Invoke("DoSomething", new object[] { myRequest});
return ((MyResponse)(results[0]));
}
When this method is called, I've noticed that the XML includes the following:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<!-- XML --!>
</soap:Body>
</soap:Envelope>
How do I remove the <soap:> wrappers from my XML?
I wouldn't. Soap is a standard protocol for publishing services and accessing remote data. Without it the remote server won't understand your request.

WCF UriTemplate with large query strings

I'm working with a rather large query string(~30+ parameters) and am trying to pass them to a WCF service I've setup.
I've run into a few issues specifically with the UriTemplate field. This service is setup to access a third party Api, so the query string may or may not contain all parameters. I'm curious if the best approach is to build a query string and pass that to the WCF service or to pass each parameter(and in some cases String.Empty) individually.
I've currently tried to dynamically build up a query string, however have hit a wall with either a 403 error when I try to pass the entire string( "?prm1=val&prm2=val" ) into the uritemplate of "ApiTool.jsp{query}", or I hit an invalid uritemplate response due to the fact I don't have name/value pairs listed.
I am pretty sure you'll need to list the parameters individually. Otherwise, UriTemplate will end up escaping things for you:
var ut = new UriTemplate("Api.jsp{query}");
var u = ut.BindByName(new Uri("http://localhost"), new Dictionary<string, string>() { { "query", "?param1=a&param2=b" } });
Console.WriteLine(u); // http://localhost/Api.jsp%3Fparam1=a&param2=b
You can 'unescape' querystring with IClientMessageInspector.
public class UriInspector: IClientMessageInspector
{
public object BeforeSendRequest(ref Message request, IClientChannel channel)
{
// change/replace request.Headers.To Uri object;
return null;
}
}
See MSDN how to add this to your Endpoint object.

Categories

Resources