Get address from service reference C# - c#

I have a project that has a service reference to a web service. Is there a way from the codebehind to get the actual http address of the service reference?
Thanks

You could retrieve it from the client proxy that was generated for you:
using (var client = new ServiceReference1.MyServiceClient("*"))
{
string address = client.Endpoint.Address.Uri.ToString();
}
or if you are having multiple endpoints in your config file:
using (var client = new ServiceReference1.MyServiceClient("MyService"))
{
var address = client.Endpoint.Address.Uri.ToString();
}

Yes, the generated proxy will have a Url property.

Related

C# WCF Service Get Status Code in Client from One way Service

I have a WCF service which has a method named ArchiveFile(string fileName) which basically archives files. I have created a proxy project using svcutil and added its reference created in my client application and is consuming the service as follows:
var binding = new WSHttpBinding { Security = new WSHttpSecurity() { Mode = SecurityMode.None } };
var address = new EndpointAddress(this.TargetUrl);
var fileService = new FileServiceClient(binding, address);'
I want to know how do I determine the Http Status Code (200 - OK or any other) for the WCF Service call.
We can get the http status code through WebOperationContext Class:
WebOperationContext statuscode = WebOperationContext.Current;
Console.WriteLine(statuscode.OutgoingResponse.StatusCode);
For more information about WebOperationContext,please refer to the following link:
https://learn.microsoft.com/en-us/dotnet/api/system.servicemodel.web.weboperationcontext?view=netframework-4.8

Transfer user information on net.tcp wcf context

I want to transfer data from client services to server services but I don't want to transfer it as parameter in each call of service functions
I tried to insert the data in OutgoingMessageProperties, but I didn't get it on server side, I got an error: A property with the name 'Token' is not present,
why?
If I'm not allowed to use it why it has the Add function?
The protocol I'm using is net.tcp
Client Side:
GeneralServicesClient Ret = new GeneralServicesClient(Consts.WcfGeneralChannels.TcpIp);
using (OperationContextScope scope = new OperationContextScope(Ret.InnerChannel))
{
OperationContext.Current.OutgoingMessageProperties.Add("Token", Guid.NewGuid());
Ret.Func();
}
Server Side:
System.ServiceModel.ServiceSecurityContext identity = System.ServiceModel.ServiceSecurityContext.Current;
OperationContext context = OperationContext.Current;
MessageProperties messageProperties = context.IncomingMessageProperties;
string token = messageProperties["Token"].ToString();//throw error: A property with the name 'Token' is not present
You should put your service call inside the using.
GeneralServicesClient Ret = new GeneralServicesClient(Consts.WcfGeneralChannels.TcpIp);
using (OperationContextScope scope = new OperationContextScope(Ret.InnerChannel))
{
OperationContext.Current.OutgoingMessageProperties.Add("Token", Guid.NewGuid());
Ret.Do();
}

Consume asmx webservice in pcl

I am developing an app which uses third party .asmx web service. And I am using PCL(Portable class Libraries) in my app.
So I wanted to consume those .asmx web services in my app. Problem is PCL doesn't support traditional web service viz .asmx. It supports WCF web services.
I have read many articles, they suggests me that from wsdl write WCF web service. But since all web services are third party, I need to write proxy in client app (Where web service is being called) such that it will convert WCF call to .asmx.
Also I have tried this example using PCL.
I am using this asmx web service
public class PerformLogIn : ILogInService
{
public string LogIn(string code)
{
ServiceReference1.WeatherSoapClient obj = new ServiceReference1.WeatherSoapClient();
obj.GetCityForecastByZIPAsync(code);
ServiceReference1.WeatherReturn get = new ServiceReference1.WeatherReturn();
return (get.Temperature);
}
But I am not getting any result.
So do anybody have idea how to do that??
Eureka I found it..
Use following code snippet
public class PerformLogIn : ILogInService
{
public void LogIn(string code)
{
ServiceReference1.WeatherSoapClient obj = new ServiceReference1.WeatherSoapClient(
new BasicHttpBinding(),
new EndpointAddress("http://wsf.cdyne.com/WeatherWS/Weather.asmx"));
obj.GetCityForecastByZIPAsync(code);
obj.GetCityForecastByZIPCompleted+=getResult;
}
void getResult(Object sender,GetCityForecastByZIPCompletedEventArgs e)
{
string error = null;
if (e.Error != null)
error = e.Error.Message;
else if (e.Cancelled)
error = "cancelled";
var result = e.Result;
}
}
So your response from web service is being stored in result variable. Just fetch the data whatever needed and return it to calling client.

WCF Callback Client Failing

I have created a WCF service which has a callback. I have created a sample client which will subscribe to these callbacks. I have been using the ListBasedPublishSubscribe sample as a base for this. However when I try and setup the unique callback address in the client with this code
context = new InstanceContext(null, new MyClass());
client = new MyClient(context);
WSDualHttpBinding binding = (WSDualHttpBinding)client.Endpoint.Binding;
string clientcallbackaddress = binding.ClientBaseAddress.AbsoluteUri;
clientcallbackaddress += Guid.NewGuid().ToString();
binding.ClientBaseAddress = new Uri(clientcallbackaddress);
The third line fails because client.Endpoint.Binding.ClientBaseAddress is null. Should this not be null (I assume so for the sample to work) and why is this the case in my app? Have I forgotten to do something?

What's wrong with my web service client?

Web service is created in PHP im calling by adding a reference in C#
funcRequest aa = new funcRequest();
aa.param = "ZZ";
string z;
funcResponse a = new funcResponse();
z = a.result;
i created like this to call the web service from C# but looks its not giving any value back .. where am i wrong ?
You shouldn't be creating the response object yourself. You should be doing something like:
FuncRequest request = new FuncRequest("ZZ");
MyWebService service = new MyWebService();
FuncResponse response = service.DoSomething(request);
Obviously the exact details will depend on how you're connecting to the service, whether you're generating the proxy code etc, but basically you need to get something involved which represents the service itself.
You'll need to instantiate and make requests with the generated client proxy class or something similar, you can't just new up requests and responses and in this manner, you need to use and retrieve them, respectively. For instance, if your service reference was named MyService then you ought to have a MyServiceClient class available to you, so that:
using (var myServiceClient = new MyServiceClient())
{
var request = new MyServiceRequestType();
request.MyProperty = "zzz";
var response = myServiceClient.MakeRequest(request);
}

Categories

Resources