Xamarin Forms Webservice soapheader always empty - forms

I need to consume asmx webservices from a Xamarin Forms app (ios and Android). Yes, I know asmx is outdated, but I can't do otherwise.
So I add the webservice with the add 'connected services' to the main Xamarin Forms project, since there is no "add web service" menu item there. I use the second menu item 'Microsoft WCF web service reference provider". I correctly select the asmx service, configure it and everything looks in order.
Now when I consume a webservice method that has a header (TestHeader class, let's say it has a TestString in it), on the webservice the header object has default values (TestString is null).
The weird thing is that if I create a similar desktop project, do the same exact configuration steps (add connected services, etc), the Reference.cs and json code generated is the exact same, but the soapheader gets correctly passed to the webservice !
[Serializable]
public class TestHeaderClass : SoapHeader
{
public int TestInt { get; set; }
public string TestString { get; set; }
}
public TestHeaderClass TestHeader { get; set; }
[WebMethod][SoapHeader("TestHeader")]
public string HelloWorld()
{
return TestHeader.TestString;
}

Related

Expose REST controller only during testing by using "test profile"

For better testability of our application I'm adding a REST controller which allows setting certain states, e.g. http://localhost:8090/test-api/customer would allow creating a new customer in the application's database for further tests.
The class would basically look like this:
#RequestScoped
#Path("/test-api/customer")
public class TestController {
#POST
public void createCustomer() {}
}
Now I'd like this controller to be available only for testing, i.e. the production system should not expose this endpoint. Is there something like a "testing profile" or "testing annotation" like a Spring Boot profile I can add to this class to hide it from the production system?

I want to expose my existing controller as a Restful service

I have an application where I'm registering a user so user will enter his data on JSP page and the data will be save into DB, the flow will be JSP->dispatcher->controller->Service->Dao.
Now in MemberController which is delegating the request to the Service, has a method register() which will take the MemberDto as a parameter now and return the Successfull msg to the success.jsp page. Sometihng like user registered successfully.
public String Register(MemberDto memberDto)
Now I want to expose this same method as RestFul service using Jersey for partners and also use this same method within my application as a normal MVC flow. How can I acheive this
So u want to use Jersey so import the jersey library to support JAX-RS.
#Path("/classlevelpath")
public class MyController {
#POST
#Produces(MediaType.APPLICATION-XML)
#Path("/register")
public String Register(MemberDto memberDto) {
}
}
Be careful JAX-RS (Jersey is an implementaion) and Spring REST annotations are different.
Annotate your rest class with #RestController. The best practice is to create another controller. By you can see this answer if you want to transform your existing controller: https://stackoverflow.com/questions/33062509/returning-view-from-spring-mvc-restcontroller

Linking to an MVC Route from Web API inside an in-memory server

I'm having problems testing the generation of MVC Routes from inside Web API. The code works when hit manually, but fails under test as the in-memory instance of Web API is unaware of the MVC routes and I can't figure out how to add them.
Here's an example project on github illustrating the problem, but I'll include some relevant code here.
I'm using an in-memory HTTP Server to host the Web API for integration testing:
private HttpConfiguration _config;
private HttpServer _server;
private HttpMessageInvoker _client;
[TestInitialize]
public void TestInitialize()
{
_config = new HttpConfiguration();
WebApiConfig.Register(_config);
_server = new HttpServer(_config);
_client = new HttpMessageInvoker(_server);
}
In my Web API Controller I'm trying to return links via the out-of-the-box default routes, both Web API and Mvc:
[HttpGet]
[Route("MvcRoute")]
public string MvcRoute()
{
return Url.Link("Default", new {Controller = "Other", Action = "Index"});
}
[HttpGet]
[Route("ApiRoute")]
public string ApiRoute()
{
return Url.Link("DefaultApi", new {Controller = "Example", Id = "MvcRoute"});
}
A test for the ApiRoute passes, but this test for the MvcRoute fails with the error message "A route named 'Default' could not be found in the route collection.":
[TestMethod]
public void ShouldReturnMvcRoute()
{
using (var request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/api/example/mvcroute"))
{
using (var response = _client.SendAsync(request, CancellationToken.None).Result)
{
var responseContent = response.Content.ReadAsStringAsync().Result;
Assert.AreEqual("\"http://localhost/Other\"", responseContent);
}
}
}
So how can I make the in-memory server aware of MVC's routes? Or if that's the wrong question to ask, how can I run automated tests on a build server (i.e., no IIS) that hit Web API routes that generate links to MVC routes?
In-memory scenario is only supported in Web API. When hosted on a real server like IIS, Web API registers its routes onto the route table provided by System.Web.Routing.RouteCollection...Since MVC also registers its routes into this same route table, when generating links from Web API to MVC, the routes are indeed there...
Note that Web API came later than MVC and one of the design goals of it was to run outside IIS host (like it can be run on Selfhost too) and not have dependency on System.Web...In your about example, you are instantiating HttpConfiguration which means that you are having a different route collection where as in the real app WebApiConfig.Register would be passed in GlobalConfiguration instance...

How do I turn off request validation in MVC2?

I needs to find a way to allow html tags in MVC2 model binding.
In MVC3 there is a way but MVC2 I dont know. can you help?
In MVC3
[AllowHtml]
public string SomeProperty{ get; set; }
I need some alternate method for AllowHtmlAttribute in mvc2
You could use the [ValidateInput(false)] attribute on the controller action:
[ValidateInput(false)]
[HttpPost]
public ActionResult SomeAction(MyViewModel model)
{
...
}
This will disable input validation for all properties of the model. There's no way in ASP.NET MVC 2 to do this per property - it has to be for the entire request.

AutoCompleteExtender not working in DotNetNuke

I am using DNN 6.0 and VS2008 AjaxControl Tool kit 3.5
I am trying to add ASP.NET AutoCompleteExtender to a text box in one of my modules in DNN.
As far as I know the AutoCompleteExtender works only through a webservice.
I've added a webservice, but I can not get the AutoCompleteExtender to work, I get no errors but the webservice never gets invoked, What am I missing or how can I get the ASP.NET AutoCompleteExtender to work?
Thank You
In my case it was the following problem:
ERROR MSG: Only Web services with a [ScriptService] attribute on the class definition can be called from script.
Try to put [ScriptService] as a definition of your web service class.
Example:
[WebService(Namespace = "http://dnndev/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[ScriptService]
// 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 BusinessDataProvider : System.Web.Services.WebService
{
[WebMethod]
[ScriptMethod]
public string[] GetSpecificListOfContributors(string prefixText, int count)
{
return ContributorController.GetSpecificListOfContributors(prefixText, count);
}
}
I hope this one will help you.
Best,