C# WCF service get client service - c#

I have a WCF service that I have added as a reference. I change the address of the service so that it does not appear in the configuration, I try to make a shortcut so that I do not write the same things every time. What would you recommend on this?
internal class TestService
{
public static TestServisClient GetServiceClient
{
get
{
var binding = new BasicHttpBinding();
var endpoind = new EndpointAddress("http://localhost:64733/TestService.svc?wsdl");
return new TestServisClient(binding, endpoind);
}
}
}
testservice.getserviceclient.getdatatables();
Is it right to use it this way, should I call it again to close the service?

i think i have to do it this way
using (TestServisClient client = new TestServisClient(
new BasicHttpBinding(), new EndpointAddress(url)))
{ ... }

Related

C# "A registration already exists for URI" XAML Warning

I'm currently working on a "server-app" / "client-app" project where the goal is to get some data from the server-app to the client app. I tried this with a WCF approach but since I've never worked with WCF it ain't an easy task for me.
So what I've already set up are the two apps in one solution à two different projects. Project one contains the server-app (TRunnerServer) and project two contains the client-app (TRunnerClient).
I've setup the interface for the service like that (in ServerApp MainWindowViewModel.cs):
[ServiceContract]
public interface ITRunnerService
{
[OperationContract]
ObservableCollection<Program> GetProgramList();
}
Than I've added the method to the class etc. like following:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
internal class MainWindowViewModel : ViewModelBase, ITRunnerService
{
public ObservableCollection<Program> GetProgramList()
{
return this.ProgramList;
}
public MainWindowViewModel()
{
var uris = new Uri[1];
string addr = "net.tcp://localhost:7000/MainWindowViewModel";
uris[0] = new Uri(addr);
ITRunnerService tRunnerService = this;
ServiceHost host = new ServiceHost(tRunnerService, uris);
var binding = new NetTcpBinding(SecurityMode.None);
host.AddServiceEndpoint(typeof(ITRunnerService), binding, string.Empty);
host.Open();
}
}
Now in the GUI of the application TRunnerClient I've got an button that I press to get the data from the TRunnerServer App.
I've just got an method binded with following:
private void Refresh(object parameter)
{
var uri = "net.tcp://localhost:7000/MainWindowViewModel";
NetTcpBinding binding = new NetTcpBinding(SecurityMode.None);
var channel = new ChannelFactory<ITRunnerService>(binding);
var endPoint = new EndpointAddress(uri);
var proxy = channel.CreateChannel(endPoint);
this.ProgramList = proxy.GetProgramList();
}
Yet when I start the app only the client starts normal and the server app gives an exception based on the error warning from the xaml "A registration already exists for URI 'net.tcp://localhost:7000/MainWindowViewModel'".
How could I solve this problem?
Note: Other questions with a similar title didn't really helped me out before someone strikes it as duplicate.
The error message indicates that there is an old server process still hanging around. Try to kill it in the task manager and try again.
In order to avoid hanging processes, make sure that you exit the application gracefully and that you don't create any windows that you don't show and close.

Exposing request method in generated Service Reference

In a generated Service Reference (imported from a WSDL), I have the following methods in the Client class, in the Reference.cs:
public Namespace.Service.SalesOrderDetail newService(Namespace.Service.Contact orderContact, Namespace.Service.Contact installationContact, string customerReference, Namespace.Service.ServiceDetails[] serviceDetailsList) {
Namespace.Service.newServiceRequest inValue = new Namespace.Service.newServiceRequest();
inValue.orderContact = orderContact;
inValue.installationContact = installationContact;
inValue.customerReference = customerReference;
inValue.serviceDetailsList = serviceDetailsList;
Namespace.Service.newServiceResponse retVal = ((Namespace.Service.ServiceRequestPortType)(this)).newService(inValue);
return retVal.salesOrder;
}
[System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced)]
System.Threading.Tasks.Task<Namespace.Service.newServiceResponse> Namespace.Service.ServiceRequestPortType.newServiceAsync(Namespace.Service.newServiceRequest request) {
return base.Channel.newServiceAsync(request);
}
public System.Threading.Tasks.Task<Namespace.Service.newServiceResponse> newServiceAsync(Namespace.Service.Contact orderContact, Namespace.Service.Contact installationContact, string customerReference, Namespace.Service.ServiceDetails[] serviceDetailsList) {
Namespace.Service.newServiceRequest inValue = new Namespace.Service.newServiceRequest();
inValue.orderContact = orderContact;
inValue.installationContact = installationContact;
inValue.customerReference = customerReference;
inValue.serviceDetailsList = serviceDetailsList;
return ((Namespace.Service.ServiceRequestPortType)(this)).newServiceAsync(inValue);
}
I've seen Python code that uses the same WSDL, and it is able to access the method as response = client.newService(request).
I'd also like to access the method in that fashion, albeit var task = client.newService(request); Task.WaitAll(task); var response = task.Result;, but I can't seem to find the right combo of creating the service reference, without being forced to have expanded input parameters to the service.
Is there a magic combo for Service Reference creation that will allow me to just pass the request as a single object?
I'm not fussed on keeping the async functionality.
The client of a service implements the interface that represents the service. It just so happens, and is shown in this example, that it doesn't necessarily make all those implemented method public.
So, to get around this, if I cast the client object to the service interface, I get to call the service as intended, regardless of what the client has made public.
var client = new ServiceClient();
var service = (Service)client;
var request = new newServiceRequest() { ... };
var response = service.newService(request);
client.Close();

WCF uses a new instance on every call, despite InstanceContextMode.Single being set

I'd like to re-use the same host instance on every WCF call. Everything I've read on the Internet says the exact same thing; use the [ServiceBehavior] attribute. So, that's what I've done:
[ServiceBehavior(InstanceContextMode = InstanceContextMode.Single)]
public class DALService : IDataAccess
{
private string _adminConnStr;
public void ServiceStart()
{
_adminConnStr = ConnectionStringManager.GetAdminDatabaseConnectionString();
}
public IEnumerable<CustomerInfo> GetCustomerInfosOrderedByShortname()
{
// _adminConnStr is null here!
}
}
I've set a breakpoint in ServiceStart to make sure that was getting run. I've set a breakpoint in GetCustomerInfo, and _adminConnStr is once again null. I've also set a watch on *this and the addresses are indeed different.
I'm wondering if this is because I create the ServiceHost objects programmatically, rather than using IIS which sets all that stuff up for you. Perhaps I need to be doing something more when I create the host? Here's my code for that:
var contract = attribute.ContractType;
var serviceHost = new ServiceHost(attribute.ServiceType ?? instance.GetType());
serviceHosts.Add(serviceHost);
var uri = String.IsNullOrEmpty(service.UriPrefix)
? new Uri(String.Format("http://{0}:{1}/{2}", serverName, port, contract.Name))
: new Uri(String.Format("http://{0}:{1}/{2}/{3}", serverName, port, service.UriPrefix, contract.Name));
if (uris.Contains(uri))
throw new DuplicateServiceRegistrationException(uri);
uris.Add(uri);
serviceHost.Description.Behaviors.Add(new ServiceMetadataBehavior
{
HttpGetEnabled = true,
HttpGetUrl = uri
});
serviceHost.AddServiceEndpoint(contract, new BasicHttpBinding(), uri);
serviceHost.Open();
instance.ServiceStart(); // This line will set _adminConnStr
Console.WriteLine("{0} now listening at: {1}", service, uri);
Figured it out.
It seems that WCF won't set your Singleton instance until the first time you call it. Basically after this line:
var serviceHost = new ServiceHost(attribute.ServiceType ?? instance.GetType());
serverHost.SingletonInstance will be null. So, when I call:
instance.ServiceStart();
I'm calling ServiceStart on an instance that WCF doesn't know about.
So, I need to set the singleton instance on WCF when I create serviceHost:
var serviceHost = new ServiceHost(instance);
Then, I can call .ServiceStart on that instance. This will fix the issue.

How to use automatically generated proxy class?

I'd like to use a web service from a database to gather informations. Right now, I implemented to web service, turned it into a proxy class via wsdl.exe but I'm slightly irritated by the outcome. The normal way to call that class is new object -> method -> parameters ->happiness. This thing only consists of partial classes and wants strange parameters. I'm not even sure if I got the right method to get the wanted information.
This seems to be the needed method:
public UniProtId2DomainIdsRecordType[] UniProtId2DomainIds (UniProtId2DomainIdsRequestRecordType UniProtId2DomainIdsRequestRecord)
{
object[] results = this.Invoke("UniProtId2DomainIds", new object[] {
UniProtId2DomainIdsRequestRecord});
return ((UniProtId2DomainIdsRecordType[])(results[0]));
}
This seems to be one of the needed classes:
public partial class UniProtId2DomainIdsRequestRecordType
{
private string uniprot_accField;
/// <remarks/>
public string uniprot_acc
{
get
{
return this.uniprot_accField;
}
set
{
this.uniprot_accField = value;
}
}
}
(That's the whole class, generated by wsdl.exe -> https://www.dropbox.com/s/yg909ibdq02js5a/GetCath.cs)
But as soon as I try to use it as I think it should work... well... my experiments on this (none of them working):
UniProtId2DomainIdsRequestRecordType Uni2Cath = new UniProtId2DomainIdsRequestRecordType();
Uni2Cath.uniprot_acc = "P0A7N9";
UniProtId2DomainIdsRecordType[] UniProtId2DomainIds;
UniProtId2DomainIdsRecordType test = new UniProtId2DomainIdsRecordType();
test.uniprot_acc = "P0A7N9";
UniProtId2DomainIdsRecordType[] UniProtId2DomainIds(test);
All I need is to get a string like P0A7N9 to be passed to the server.
(The reference to this webservice: http://api.cathdb.info/api/soap/dataservices/wsdl#op.o159501052 )
Can someone give me a hint how to handle this, please?
The easiest way would be to add this web service as Service Reference to your project. Then you can call the different methods. Use this as the address: http://api.cathdb.info/api/soap/dataservices/wsdl
using (var ser = new DataServicesPortTypeClient())
{
var results = ser.UniProtId2DomainIds(new UniProtId2DomainIdsRequestRecordType
{
uniprot_acc = "P0A7N9"
});
if (results != null)
{
var geneName = results.gene_name;
var speciesName = results.species_name;
}
}
If you want to use your generated class do this:
using (var service = new DataServices())
{
var results = service.UniProtId2DomainIds(new UniProtId2DomainIdsRequestRecordType
{
uniprot_acc = "P0A7N9"
});
if (results != null && results.Length >0)
{
var geneName = results[0].gene_name;
var speciesName = results[0].species_name;
}
}
As John suggested in the comments, ASMX and wsdl.exe are deprecated technologies. You should be using Service References and svcutil.exe

WCF MessageHeaders in OperationContext.Current

If I use code like this [just below] to add Message Headers to my OperationContext, will all future out-going messages contain that data on any new ClientProxy defined from the same "run" of my application?
The objective, is to pass a parameter or two to each OpeartionContract w/out messing with the signature of the OperationContract, since the parameters being passed will be consistant for all requests for a given run of my client application.
public void DoSomeStuff()
{
var proxy = new MyServiceClient();
Guid myToken = Guid.NewGuid();
MessageHeader<Guid> mhg = new MessageHeader<Guid>(myToken);
MessageHeader untyped = mhg.GetUntypedHeader("token", "ns");
OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
proxy.DoOperation(...);
}
public void DoSomeOTHERStuff()
{
var proxy = new MyServiceClient();
Guid myToken = Guid.NewGuid();
MessageHeader<Guid> mhg = new MessageHeader<Guid>(myToken);
MessageHeader untyped = mhg.GetUntypedHeader("token", "ns");
OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
proxy.DoOtherOperation(...);
}
In other words, is it safe to refactor the above code like this?
bool isSetup = false;
public void SetupMessageHeader()
{
if(isSetup) { return; }
Guid myToken = Guid.NewGuid();
MessageHeader<Guid> mhg = new MessageHeader<Guid>(myToken);
MessageHeader untyped = mhg.GetUntypedHeader("token", "ns");
OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
isSetup = true;
}
public void DoSomeStuff()
{
var proxy = new MyServiceClient();
SetupMessageHeader();
proxy.DoOperation(...);
}
public void DoSomeOTHERStuff()
{
var proxy = new MyServiceClient();
SetupMessageHeader();
proxy.DoOtherOperation(...);
}
Since I don't really understand what's happening there, I don't want to cargo cult it and just change it and let it fly if it works, I'd like to hear your thoughts on if it is OK or not.
I think your refactored code doesn't put any added-value. Have you taken in account that the OperationContext can be null?
I think this will be a safer approach:
using(OperationContextScope contextScope =
new OperationContextScope(proxy.InnerChannel))
{
.....
OperationContext.Current.OutgoingMessageHeaders.Add(untyped);
proxy.DoOperation(...);
}
OperationContextScope's constructor will always cause replacement of the Operation context of the current thread; The OperationContextScope's Dispose method is called which restores the old context preventing problems with other objects on the same thread.
I believe your OperationContext is going to get wiped each time you new the proxy.
You should plan on adding the custom message headers prior to each call. This is good practice in any case as you should prefer per call services and close the channel after each call.
There are a couple patterns for managing custom headers.
You can create the header as part of the constructor to the proxy.
Alternatively, you can extend the binding with a behavior that automatically adds the custom header prior to making each call. This is a good example: http://weblogs.asp.net/avnerk...

Categories