why changing wcf endpoint address in code seems doesn't work? - wcf

We have some really legacy wcf code and we got a strange problem:
WCFChartService.ChartServiceClient service = new WCFChartService.ChartServiceClient();
using (new OperationContextScope(service.InnerChannel))
{
service.Endpoint.Address = new System.ServiceModel.EndpointAddress(
"http://mypreprodsite.net/MyChartService.svc");
service.UpdateChartTemplate(
xxxx);
}
In web.config, we have:
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IChartService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://myprodsite.net/MyChartService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IChartService"
contract="WCFChartService.IChartService" name="BasicHttpBinding_IChartService" />
</client>
</system.serviceModel>
Above code throws some error, so I debugged this, and turned out if in my web.config I have this everything will be fine
<endpoint address="http://mypreprodsite.net/MyChartService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IChartService"
contract="WCFChartService.IChartService" name="BasicHttpBinding_IChartService" />
The function change in wcf has only been deployed to preprod site, so looks like in my testing project, changing url directly in service.Endpoint.Address doesn't work as I expected.
As I moved away from old wcf code years ago (only randomly got assigned to look this one), can someone explain why? If I set service.Endpoint.Address="localhost:8049/MyChartService.svc" and my local machine does get called, so I am rather confused.

There are two ways to specify endpoint addresses for a service in WCF. You can specify an absolute address for each endpoint associated with the service or you can provide a base address for the ServiceHost of a service and then specify an address for each endpoint associated with this service that is defined relative to this base address.
Here is the reference: Specifying an Endpoint Address.

Related

How to consume WCF Service in Orchard

I am trying to consume a service in my module in the module web config I added my service config as the following.
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="WSEventSoap" />
<binding name="BasicHttpBinding_ITwitterService" />
<binding name="BasicHttpBinding_ILoyalty">
<security mode="Transport" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint name="BasicHttpBinding_ITwitterService"
address="wwww.mysite.com/MediaServices/TwitterService.svc"
binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_ITwitterService"
contract="TwitterService.ITwitterService" />
</client>
</system.serviceModel>
within the modules web.config. What I am noticing is that
I can't seem to access web config settings in my module
I keep getting the following error.
Could not find default endpoint element that references contract 'TwitterService.ITwitterService' in the ServiceModel client configuration section. This might be because no configuration file was found for your application, or because no endpoint element matching this contract could be found in the client element.
Please help.
On the site I'm currently working on, like Betrand suggests, we've avoided modifying the global web.config to ensure the module is portable between projects.
Instead we've just created the Binding and Endpoint in code, rather that them getting pulled from the config. We've added our own setting to the Orchard site settings to let the user specify the endpoint address via the admin dashboard, which is the only part we actually need to be configurable.
To do this, in the module add a service reference as normal, if it adds those sections to your module web.config remove them as you won't be using them.
Then in code do something like
Binding binding = new BasicHttpBinding();
EndpointAddress address = new EndpointAddress("http://localhost:8050/MyService/");
using (var client = new MyServiceClient(binding, endpointAddress))
{
client.MyMethod();
}
There's plenty examples of this on Stackoverflow and elsewhere (e.g. WCF: How can I programatically recreate these App.config values?)

How do I set the proxy in webhttpbinding configuration to use fiddler

I'm trying to setup my Web service client to use fiddler proxy by setting it in the webhttpbinding configuration file as follows:
<bindings>
<webHttpBinding>
<binding name="RestBinding" proxyAddress="http://localhost:8888"
useDefaultWebProxy="false">
</binding>
</webHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:2172/RestServiceImpl.svc"
binding="webHttpBinding" behaviorConfiguration="RestService"
bindingConfiguration="RestBinding"
contract="IWS.IRestServiceImpl" name="Rest"/>
</client>
This doesn't seem to work. I don't see anything logged in fiddler when calling the webservice from the client. I know the call is being made because I get a response back from the service.
I can't seem to figure out what I'm doing wrong.
Any help is appreciated!
Thanks!!
The .NET Framework does not send requests for "localhost" to any proxy, including Fiddler. To workaround this, try to use your machine name instead as the endpoint address (e.g. http://mymachine:2172/RestServiceImpl.svc).
Also, take a look on Using Fiddler with IIS7 Express

WCF Service WindowsPhone - EndpointNotFoundException

I'm developing solution within WCF Service and Windows Phone client application. The problem is I cannot connect to service even that in emulator when I type address of service in Internet Explorer I get right result.
My config file:
<configuration>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMyCustomService"
maxReceivedMessageSize="2147483647"
maxBufferSize="2147483647"
enableHttpCookieContainer="true"
/>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:2395/MyCustomService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyCustomService"
contract="MyService.IMyCustomService" name="BasicHttpBinding_IMyCustomService" />
</client>
</system.serviceModel>
</configuration>
My service interface:
[ServiceContract]
[ServiceKnownType(typeof(CustomResponse))]
public interface IMyCustomService
{
[OperationContract]
CustomResponse GetData();
}
My Problem is that each time i try to call on client proxy GetDataAsync() method, the completed event is not fire up and I get "EndpointNotFoundException". I've tried all solution which I found and none of them helped me. I also try to make WPF test client and it works correctly but Windows Phone app does not.
I assume this is your client configuration, e.g. the phone. Your pointing to localhost, but since you´re in the emulator on the phone localhost resolves to the same, not your PC where the service is hosted.
address="http://localhost:2395/MyCustomService.svc"
Put your PC´s hostname in there and you should be fine
Hm, I have no idea how it started work but it did. I upload my service to internet hosting and delete all service reference from my phone client project and add them again with internet address and it works now. Thx for your help Dominik.

WCF programmatically set endpoint address: No elements matching the key were found in the configuration element collection

I'm performing what I believed was a pretty basic task. We have an environment with multiple servers (DEV, TEST, PRODUCTION) and I'd like to programmatically change the service endpoint. To do this I am creating a new EndPointAddress and instantiating the client as:
BasicHttpBinding binding = new BasicHttpBinding("BasicHttpBinding_IMyService");
EndpointAddress endpoint = new EndpointAddress(new Uri("http://domain.name/myservice.svc"));
MyService.MyServiceClient client = new MyService.MyServiceClient(binding, endpoint);
I am receiving the following error.
No elements matching the key 'BasicHttpBinding_IMyService' were found in the configuration element collection.
I have included my app.config below but, as you can see, I do have the binding defined.
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IMyService" >
<... removed directives for ease of reading ...>
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://domain.name/MyService.svc"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IMyService"
contract="MyService.IMyService" name="BasicHttpBinding_IMyService" />
</client>
</system.serviceModel>
I'm sorry if this is a simple question but I haven't been able to identify the problem. I'll call it the 'late-Friday brain fog', and maybe you can call it 'easy points'?
Thanks!
The bindings in the Web.config of the WCF application and the app.config of the client application must match

How do I call a WCF webservice from Silverlight?

I am trying to call a WCF webservice (which I developed) from a Silverlight application. For some reason the Silverlight app does not make the http soap call to the service. I know this because I am sniffing all http traffic with Fiddler (and it is not a localhost call).
This my configuration in the server relevant to WCF:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true"/>
<services>
<service behaviorConfiguration="ServiceBehavior" name="Service">
<endpoint address="" binding="basicHttpBinding" contract="Service"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
</system.serviceModel>
And the ServiceReferences.ClientConfig file in the silverlight app (i am using the beta 2):
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_Service" maxBufferSize="65536"
maxReceivedMessageSize="65536">
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://itlabws2003/Service.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_Service" contract="Silverlight_organigram.DataService.Service"
name="BasicHttpBinding_Service" />
</client>
</system.serviceModel>
This is the silverlight method that calls the service, I paste the whole method for copleteness, the lambda is to make the call synchronous, I have debugged it and after the line client.GetPersonsAsync(), Fiddler does not show any message travelling to the server.
public static List<Person> GetPersonsFromDatabase()
{
List<Person> persons = new List<Person>();
ServiceClient client = new ServiceClient();
ManualResetEvent eventGetPersons = new ManualResetEvent(false);
client.GetPersonsCompleted += new EventHandler<GetPersonsCompletedEventArgs>(delegate(object sender, GetPersonsCompletedEventArgs e)
{
foreach (DTOperson dtoPerson in e.Result)
{
persons.Add(loadFromDto(dtoPerson));
}
eventGetPersons.Set();
});
client.GetPersonsAsync();
eventGetPersons.WaitOne();
return persons;
}
Does anyone have any suggestions how I might fix this?
If the Silverlight application is not hosted in the same domain that exposes the Web service you want to call, then cross-domain restrictions applies.
If you want the Silverlight application to be hosted in another domain than the web service, you may want to have a look on this post to help you to have a cross domain definition file, or to write a middle "proxy" instead.
You wouldn't happen to be running from the filesystem would you? If you are serving up the silverlight application your local machine and not using the VS Web Server or IIS, you won't be able to make HTTP calls for security reasons. Similarly if you're loading from a web server, you can't access local resources.
Also I've found that Nikhil's Web Development Helper http://www.nikhilk.net/ASPNETDevHelperTool.aspx can be more useful than Fiddler because you will see local traffic as well, although it doesn't look like that is your issue in this case.
I am not 100% certain, but if you are running on Vista or Server 2008 you may have run into the User Access Control issue with http.sys
So in Vista and Win2k8 server, the HttpListener will listen only if you are running under a high privelege account. In fact, from my experience, even if you add yourself to the local administrators group, you might run into this issue.
In any case, try launching Visual Studio on Vista by Right Clicking and runas Administrator. See if that fixes it. If it does, you're good, but....
ideally you should run httpcfg
like:
httpcfg set urlacl -u http://itlabws2003 -a D:(A;;GX;;;yoursid)
your sid = the security identifier for the account you're running as, you can find it here:
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
if you don't know it already, or you could possibly add yourself to BUILTIN\Administators, find the sid and run the httpcfg via command line again, specifying that sid.
User Access Control, Vista and Http.sys cause all this...if this is indeed the problem you are running into. Not sure but maybe its worth a try