I have code that use soap client.
using (webTest.TestSoapClient sc = new webPrint.TestSoapClient())
{
sc.Print();
}
I have the following in web.config
<client>
<endpoint address="http://localhost/webservice/test.asmx"
binding="basicHttpBinding" bindingConfiguration="TestSoap"
contract="webTest.TestSoap" name="TestSoap" />
</client>
Now I want to add more endpoints to the web.config. like
<endpoint address="http://example.com/webservice/test.asmx"
binding="basicHttpBinding" bindingConfiguration="TestSoap"
contract="webTest.TestSoap" name="TestSoap1" />
<endpoint address="http://123.com/webservice/test.asmx"
binding="basicHttpBinding" bindingConfiguration="TestSoap"
contract="webTest.TestSoap" name="TestSoap2" />
</client>
Can I create the soap client in code using different endpoint like follows:
using (webTest.TestSoapClient sc = new webPrint.TestSoapClient("TestSoap1"))
{
sc.Print();
}
using (webTest.TestSoapClient sc = new webPrint.TestSoapClient("TestSoap2"))
{
sc.Print();
}
using (webTest.TestSoapClient sc = new webPrint.TestSoapClient("TestSoap3"))
{
sc.Print();
}
Is that feasible, and how can I do it?
Related
I'd like to make WCF service which can put image into stream.
I have next in config:
<service name="Images" behaviorConfiguration="ImagesBehavior">
<endpoint address="http://localhost:5523/Images.svc"
binding="basicHttpBinding" contract="Images" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:5523/Images" />
</baseAddresses>
</host>
</service>
<behavior name="ImagesBehavior">
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceMetadata httpGetEnabled="true" />
</behavior>
And code:
[OperationContract]
[WebGet(UriTemplate = "GetImage/{imageID}",
BodyStyle = WebMessageBodyStyle.Bare)]
public Stream GetImage(string imageID)
{
try
{
if (WebOperationContext.Current != null)
WebOperationContext.Current.OutgoingResponse.ContentType = "image/jpeg";
var ms = new MemoryStream(myImage);
return ms;
}
catch (Exception e)
{
if (WebOperationContext.Current != null)
WebOperationContext.Current.OutgoingResponse.ContentType = "text/xml";
Console.WriteLine("GetImage ERROR:" + e.Message + "\r\n" + e.StackTrace);
byte[] errorBytes = Encoding.UTF8.GetBytes("ERROR");
return new MemoryStream(errorBytes);
}
}
But when I'm trying this via browser like
http://localhost:5523/Images.svc/GetImage/imagecodeblabla
I've got
400 Bad Request
And when
http://localhost:5523/Images/GetImage/imagecodeblabla
405 Method Not Allowed
What's wrong?
Is your service SOAP or REST? It appears that you're using REST syntax (WebGetAttribute) but your binding is BasicHttpBinding (SOAP).
Try using WebHttpBinding instead and see how that goes!
I m trying to create end point configuration Programmatically, below is the config file generated, I want to create same configuration using c# programmatically.I want to set bindingConfiguration,contract,name,binding,address through programatically.
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IFakeService" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:55536/FakeService.svc" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFakeService" contract="ChannelAdam.Wcf.BehaviourSpecs.TestDoubles.IFakeService" name="BasicHttpBinding_IFakeService" />
</client>
</system.serviceModel>
var binding = new BasicHttpBinding() {
Name = "BasicHttpBinding_IFakeService",
MaxBufferSize = 2147483647,
MaxReceivedMessageSize = 2147483647
};
var endpoint = new EndpointAddress("http://localhost:55536/FakeService.svc");
MyInterfaceClient client = new MyInterfaceClient(binding, endpoint);
I was trying an example partially from Chapter 5 - RESTful .Net, but couldn't make it work for some reason (receiving 404-Not found).
using System;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.ServiceModel.Web;
[ServiceContract]
public class RestService
{
[OperationContract]
[WebGet(UriTemplate = "Hosting")]
public void Hosting()
{
Console.WriteLine("RestService::Hosting()");
WebOperationContext.Current.OutgoingResponse.StatusCode = System.Net.HttpStatusCode.OK;
}
static void Main(string[] args)
{
var host = new ServiceHost(typeof(RestService));
var endpoint = host.AddServiceEndpoint(typeof(RestService), new WebHttpBinding(), "http://localhost:8080/Hosting");
endpoint.Behaviors.Add(new WebHttpBehavior());
host.Open();
Console.ReadKey();
}
}
It works (returns status-code OK) if I use WebServiceHost as follows
static void Main(string[] args)
{
var host = new WebServiceHost(typeof(RestService), new Uri("http://localhost:8080"));
host.Open();
Console.ReadKey();
}
So the question is how to make it work with ServiceHost (without any configuration file etc. if possible) ?
WebServiceHost creates an EndPoint for you, nothing wrong if you continue using it. Refer this link for more details...
But you can also add below configuration to your service configuration to use ServiceHost, I have given an example, you can change it to reflect your service classes.
<system.serviceModel>
<services>
<service name="YourService.DateTimeService" behaviorConfiguration="customBehavior">
<endpoint address="Basic" binding="basicHttpBinding" contract="DifferentBindings.IDateTime">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="Web" binding="webHttpBinding" contract="DifferentBindings.IDateTime" behaviorConfiguration="webHttpBehavior">
<identity>
<dns value="localhost"/>
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/DifferentBindings/DateTimeService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false before deployment -->
<serviceMetadata httpGetEnabled="True"/>
<!-- To receive exception details in faults for debugging purposes,
set the value below to true. Set to false before deployment
to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webHttpBehavior">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
So I was curious how complex this will be with WCF. Tried it out. It's streight forward.
I used this tutorial to create a simple service that has a doWork method that expects a string and returns a greeting.
Greeting:
[DataContract]
public class Greeting
{
[DataMember]
public string Str { get; set; }
}
Svc Contract:
[OperationContract]
[WebInvoke(Method = "ResponseFormat = WebMessageFormat.BodyStyle = WebMessageBodyStyle.Bare, UriTemplate = "sayHello/{name}/")]
Greeting DoWork(string name);
Svc Impl:
public class GreetingService : IGreetingService
{
public Greeting DoWork(string name)
{
return new Greeting {Str = string.Format("Hello {0}", name)};
}
}
Then you can first test it by:
Right click on the svc file in visual studio > view in browser
add 'sayHello/test' to the url
see the greeting in the browser
A consumer for this can be implemented in AngularJS using either the $http oder the $resource service
<!DOCTYPE html>
<html ng-app="restTest">
<head>
<title></title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.27/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.27/angular-resource.min.js"></script>
<script language="javascript">
// set up the app
var app = angular.module('restTest', ['ngResource']);
// create a controller
app.controller('RestTestCtrl', function ($scope, $http, $resource) {
// initial greeting
$scope.greeting = "Not greeted yet";
// say hello, consuming the svc using $http
$scope.sayHelloHttp = function () {
var url = "http://localhost:7507/GreetingService.svc/sayHello/" + $scope.inName + "/";
$http.get(url).
success(function(data) {
$scope.greeting = data.Str;
});
}
// say hello, consuming the svc using $resource
$scope.sayHelloRest = function () {
var GreetingSvc = $resource("http://localhost:7507/GreetingService.svc/sayHello/:name");
GreetingSvc.get({ name: $scope.inName }, function(data) {
$scope.greeting = data.Str;
});
}
});
</script>
</head>
<body ng-controller="RestTestCtrl">
<!-- bind the value of this input to the scope -->
<input type="text" ng-model="inName"/>
<button ng-click="sayHelloHttp()">$http</button>
<button ng-click="sayHelloRest()">$resource</button>
<!-- bind the greeting property -->
<div>{{greeting}}</div>
</body>
</html>
I guess everything above can be expressed more advance but it should give you a basic and working example to get started.
I have a situation where I would like to make a WCF call as another call is coming in.
Site1 request--> Site2
Site2 request --> Site3
Site2 <-- Site3 response
Site1 <-- Site2 response
The problem I am having is that when Site2 tries to send a message to Site3 while Site1 is sending to Site2; Site2 says it cannot find Site3.
The actual error message is:
Could not find endpoint element with name 'Endpoint_IEchoService' and contract
'FakeCompany.API.Services.Contract.IEchoService' 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 name could be
found in the client element.
Each site is the same configuration and code base. The client, proxy and server are all in the same project. The apps are clones calling each other. It is one website with multiple address bindings. Other regular calls between the sites work fine until I try a call within a call.
As you can probably guess from the contact name, not much in the complex way is happening in my echo service. Single echo calls between the sites work. My problem is when i make a cascade call on the service side to another site.
I am wondering if this is not allowed or if a configuration setting change is required.
Some code and config.
Endpoint addresses are changed at runtime.
If you see something "funky", it is because the client, proxy and service inherit from generic base classes.
//-- ServiceModel Client
<endpoint address="http://FakeCompany.unittest/Services/EchoService.svc"
binding="basicHttpBinding" bindingConfiguration="SecureBinding"
contract="FakeCompany.API.Services.Contract.IEchoService" name="Endpoint_IEchoService">
<identity>
<servicePrincipalName value="host/mikev-ws" />
</identity>
</endpoint>
//-- Bindings
<bindings>
<basicHttpBinding>
<binding name="SecureBinding"
maxReceivedMessageSize="10000000"
sendTimeout="00:05:00">
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
</bindings>
//-- Behaviours
<behaviors>
<serviceBehaviors>
<behavior name="MyServiceTypeBehaviors">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
</behaviors>
//-- Services
<service name="FakeCompany.API.Services.Service.EchoService" behaviorConfiguration="MyServiceTypeBehaviors">
<endpoint address="" binding="basicHttpBinding" contract="FakeCompany.API.Services.Contract.IEchoService" bindingConfiguration="SecureBinding" />
</service>
//-- TEST
[Test]
public void CascadeMessage()
{
//-- TEST: That a wcf call can occur within another wcf call.
//-- ARRANGE
DTO_Echo_Cascade_Request request = new DTO_Echo_Cascade_Request(unit1, unit2);
request.NextCall = string.Format("{0};{1};{2};", unit3, unit4, unit5);
//-- ACT
var response = EchoAgent.Cascade(request);
//-- ASSERT
Assert.AreEqual("TBA", response.Response);
}
//-- AGENT
internal static DTO_Echo_Response Cascade(DTO_Echo_Cascade_Request request)
{
DTO_Echo_Response response;
using (EchoServiceClient serviceClient = new EchoServiceClient(request))
{
response = serviceClient.Cascade(request);
}
return response;
}
//-- CLIENT
public DTO_Echo_Response Cascade(DTO_Echo_Cascade_Request request)
{
return Process(() => Proxy.Cascade(request));
}
CONTRACT, DTO, PROXY are omitted.
//-- SERVICE
public DTO_Echo_Response Cascade(DTO_Echo_Cascade_Request request)
{
DTO_Echo_Response response = new DTO_Echo_Response();
response.Response += string.Format("Hello from {0};", request.TargetAddress);
if (request.NextCall.NotNullOrEmpty())
{
var split = request.NextCall.Split(new [] {';'}, StringSplitOptions.RemoveEmptyEntries);
if (split.GetUpperBound(0) > 0)
{
DTO_Echo_Cascade_Request nextRequest = new DTO_Echo_Cascade_Request(request.TargetAddress, split[0]);
for (int i = 1; i < split.GetUpperBound(0); i++)
{
nextRequest.NextCall += split[i] + ";";
}
response.Response += EchoAgent.Cascade(nextRequest).Response;
}
}
return response;
}
The exception occurs on the following line
response.Response += EchoAgent.Cascade(nextRequest).Response;
How can I code a c# sample for reading my Client endpoint configurations:
<client>
<endpoint address="http://mycoolserver/FinancialService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IFinancialService"
contract="IFinancialService" name="WSHttpBinding_IFinancialService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="http://mycoolserver/HumanResourcesService.svc"
binding="wsHttpBinding" bindingConfiguration="WSHttpBinding_IHumanResourceService"
contract="IHumanResourceService" name="WSHttpBinding_IHumanResourceService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
And the goal is to obtain an array of endpoints address:
List<string> addresses = GetMyCurrentEndpoints();
As result we would have:
[0] http://mycoolserver/FinancialService.svc
[1] http://mycoolserver/HumanResourcesService.svc
This is my first answer ever. Be gentle :)
private List<string> GetMyCurrentEndpoints()
{
var endpointList = new List<string>();
var config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
var serviceModel = ServiceModelSectionGroup.GetSectionGroup(config);
foreach (ChannelEndpointElement endpoint in serviceModel.Client.Endpoints)
{
endpointList.Add(endpoint.Address.ToString());
}
return endpointList;
}
// Automagically find all client endpoints defined in app.config
ClientSection clientSection =
ConfigurationManager.GetSection("system.serviceModel/client") as ClientSection;
ChannelEndpointElementCollection endpointCollection =
clientSection.ElementInformation.Properties[string.Empty].Value as ChannelEndpointElementCollection;
List<string> endpointNames = new List<string>();
foreach (ChannelEndpointElement endpointElement in endpointCollection)
{
endpointNames.Add(endpointElement.Name);
}
// use endpointNames somehow ...
(Taken from http://mostlytech.blogspot.com/2007/11/programmatically-enumerate-wcf.html)