How to call a ASP.Net C# WCFService on Android Studio? - c#

Can anyone help me on how to call a C# WCF web service from Android Studio. My webservice is "http://192.168.1.100:93/MyService.svc?wsdl"
MyService.svc
namespace MyServiceApp
{
public class MyService : IMyService
{
public string Hello(string strName)
{
string strResult="";
strResult = "Hello " + strName;
return strTest;
}
}
}
IMyService.cs
namespace MyServiceApp
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
string Hello(string strName);
}
}

There is no direct way to connect to WCF service from Android studio, but you can still consume it (WCF Service) as SOAP/POST.
To get you started - look at previous answer by Andy white - I'm using this approach as well and it is working quite well.
Note : You might want to inspect packets that service sends/gets via some tool like fiddler.
Alternatively for people that are using Xamarin (I know that it doesn't help in your situation, but it might be usefull for other people) - I would look at microsoft documentation (or SO if you like it more).

Related

Consuming a Soap Client in Xamarin Visual Studio 2017 - Dublin Bus Real Time Information

I would appreciate some help, I am fairly new to programming.
I am trying to consume a soap client in Xamarin using Visual Studio 2017.
The Soap Client is for the real time information for a bus service.
From Researching the topic I understand adding the 'WSDL' as a web reference to VS 2017, I am just wondering the best way to call the information into the Xamarin application.
http://rtpi.dublinbus.ie/DublinBusRTPIService.asmx?WSDL
I have added the Soap Client (WSDL) as a web reference (above)
I have attempted to consume the data using C#
I have created a interface
namespace Soap
{
public interface RealTimeInfo
{
bool GetRealTimeStopData(string data);
}
}
I created a get 'RealTimeInfo' class
[assembly:Dependency(typeof(Soap.Droid.GetRealTimeInfo))]
namespace Soap.Droid
{
public class GetRealTimeInfo : RealTimeInfo
{
public bool GetRealTimeStopData(string data)
{
var RTPIService = new ie.dublinbus.rtpi.DublinBusRTPIService();
bool returnoRealTimeStopData = RTPIService.GetAllDestinations(data);
return returnoRealTimeStopData;
}
}
}
Any help would be really appreciated.

Connecting Android app to ASP.NET web service: java.net.MalformedURLException: Protocol not found

I am learning how to connect an Android app to ASP.NET web service from an online tutorial. When learning, I replaced their web service with one I had written on my own and ran it on a PC from Visual Studio. Then, I called the method 'Add' in the web service from the Android app but I am getting this error:
java.net.MalformedURLException: Protocol not found
I believe there is something wrong with the SOAP_ADDRESS field below.
public final String SOAP_ACTION = "http://tempuri.org/Add";
public final String OPERATION_NAME = "Add";
public final String WSDL_TARGET_NAMESPACE = "http://tempuri.org/";
public final String SOAP_ADDRESS = "12.145.142.45:50428/WebService.asmx";
This is the code of my web service.
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 WebService : System.Web.Services.WebService
{
public WebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public int Add(int a, int b)
{
return a+b;
}
}
I think the error is showing up because I cannot specify IP addresses like that directly in Java code. So, my question is what is the correct SOAP_ADDRESS value for connecting the Android app to my ASP.NET web service?

Find all references with WCF OperationContract and DataContracts

I'm trying to figure out if there's a way to "Find all references" (using the VS feature, as opposed to Control+F entire solution). when it comes to WCF Data and OperationContracts. In case that is unclear:
namespace WcfTestReferences
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello world");
DoStuff();
ServiceReference1.Service1Client client = new ServiceReference1.Service1Client();
var results = client.GetData(42);
Console.WriteLine(results);
}
static void DoStuff() { }
}
}
namespace WcfTestReferences.WCFApp
{
[ServiceContract]
public interface IService1
{
[OperationContract]
string GetData(int value);
}
public class Service1 : IService1
{
public string GetData(int value)
{
return string.Format("You entered: {0}", value);
}
}
}
Solution looks like this:
Now, if I look at DoStuff() with code lens, I can see that it in fact has a reference to it:
But the same does not hold true for the methods being called in the wcf service:
In the above, the only references to the interface/method is the interface/method. I understand that the reference that I was hoping would be there (from the main method):
var results = client.GetData(42);
is not there, because the client is generated, and is not actually my Service1 implementation... but is there a way to change this?
In the real world, we have a WCF layer with thousands of methods, many of which are not used - but I cannot rely on Code Lens/Find all references to make this determination. Is there any way to change this behavior?
because the client is generated, and is not actually my Service1
implementation
This is the root of the problem.
You are correct - there is no way for your code analyser to determine that the GetData() call you are making from your client is semantically the same thing as the GetDate() service operation you have defined on your interface, because from a binary perspective they are defined in two completely different types.
The root of this is that you're using a service reference. WCF provides service references as the default way of connecting to a service, but in my opinion service references are problematic and should be avoided.
Luckily, WCF provides another way of consuming and calling a service via the user of ChannelFactory<T>. One of the many benefits you will get when using this instead of a service reference is that your client will have use of the service interface via a binary reference to the assembly containing your service definition.
This will allow tools like code lens to resolve references to your interface methods directly to your consuming clients.

ServiceStack - how to host multiple versioned endpoints in one service?

Where I was
I'm trying to convert some WCF services to use ServiceStack instead. For the most part it's achieving what I want but there's definitely differences. eg with WCF I had something like:
interface IMethod1{ ResultDTO Method1(InputDTO input); }
interface IMethod2{ ResultDTO Method2(InputDTO input); }
interface IMethod3{ ResultDTO Method3(InputDTO input); }
interface IMyService : IMethod1, IMethod2, IMethod3
then implement with:
public class MyService : ServiceBase, IMyService { /* ... */ }
Where I'm at
With ServiceStack it's more like:
public class Method1{
// parameters for method as properties
}
public class Method2{
// parameters for method as properties
}
public class Method3{
// parameters for method as properties
}
I've tried various thing and the latest dead-end I've hit was with:
public class MyServiceHost<T> : AppHostBase
{
public MyServiceHost(string version)
: base("My Service v" + version, typeof(T).Assembly)
{ }
public override void Configure(Funq.Container container){
Routes.AddFromAssembly(typeof(T).Assembly);
}
}
protected void Application_Start(object sender, EventArgs e) {
new MyServiceHost<Foo.Bar.V0101.MyService>("1.1").Init();
new MyServiceHost<Foo.Bar.V0102.MyService>("1.2").Init();
new MyServiceHost<Foo.Bar.V0201.MyService>("2.1").Init();
}
where it complains that AppHost has already been initialised.
Where I want to be
I want to expose something like this:
http://www.sandwich.com/example/v0101/sandwichservice.wsdl
http://www.sandwich.com/example/v0102/sandwichservice.wsdl
http://www.sandwich.com/example/v0201/sandwichservice.wsdl
or
http://www.sandwich.com/example/sandwich_v0101.wsdl
http://www.sandwich.com/example/sandwich_v0102.wsdl
http://www.sandwich.com/example/sandwich_v0201.wsdl
ideally hosted in the same service process.
So is there a simple answer I'm missing or am I approaching the whole thing fundamentally wrong? Or in a nutshell: using ServiceStack, is it possible to and how can I expose multiple endpoints and WSDLs for versioned web services in the same host service?
See this answer for recommended versioning strategies with ServiceStack.
You can't expose multiple versions of SOAP/WSDL's in ServiceStack, you're encouraged to evolve the same DTO's which means there are no previous type versions to create an older version of the WSDL. You would need to host older versions of ServiceStack project for the auto-generated WSDL to match up with older types.
You could also take a snapshot of a WSDL and host it statically, but whether a new SOAP endpoint accepts a client sending an old SOAP version is up to .NET's WCF Message class doing the parsing. But as SOAP is a brittle format, YMMV.

WCF REST service doesn't work on an IIS6 virtual server

Problem is I have a very simple WCF REST service, which I wrote starting from the WCF Service application template.
I have one method, one class set up like this
[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MainService
{
[WebGet(UriTemplate = "{ricCode}")]
public IdentifierInfo GetByRicCode(string ricCode)
{
...
}
}
When ran from my machine I have no problems it works fine (typical).
My problem is that when I publish this to a website on IIS6 (set up for anonymous access and on a virtual server) all I get from the above method is a 400 - invalid request.
When I changed the method as a test to this
[WebGet(UriTemplate = "")]
public string GetByRicCode()
{
return "foo";
}
and ran in on the IIS6 server it worked fine.
Maybe I set up the virtual server wrong on IIS... any ideas please?
I figured out the problem, it was throwing an exception due to nested web.configs
How annoying.

Categories