I'm learning WCF, I started creating a very basic host app which defines a class with one method as follows:
[ServiceContract()]
public interface IMath
{
[OperationContract]
int Add(int a, int b);
}
public class MathCalcs : IMath
{
public MathCalcs()
{
Console.WriteLine("Service await two numbers...");
}
public int Add(int a, int b)
{
return a + b;
}
}
and that is how I configured the App.config file:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="ConsoleHost.MathCalcs" behaviorConfiguration="MathServiceMEXBehavior">
<endpoint address="http://localhost:8080/MathCalcs"
binding="basicHttpBinding"
contract="ConsoleHost.IMath"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/MathCalcs"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MathServiceMEXBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>
Then I called the service from Main
using (ServiceHost host = new ServiceHost(typeof(MathCalcs)))
{
host.Open();
Console.WriteLine("***The service is ready***");
}
Console.ReadLine();
But it fails to view the metadata of the service through the URI http://localhost:8080/MathCalcs, I'm sure I'm following the steps right as the book I'm reading from and as a preceding example works fine, the only difference is that I didn't separate the service logic (the interface and the class) in a stand alone class library.
What am I missing?
The following line of code
Console.ReadLine();
must be inside the braces of the using clause!
When that is done, retry finding the WSDL metadata.
Here is a related post that I think may lead you in the right direction.
WCF service showing blank in browser
Related
I have a simple WCF service:
namespace Vert.Host.VertService
{
[ServiceContract]
public interface IRSVP
{
[OperationContract]
bool Attending();
[OperationContract]
bool NotAttending();
}
public class RSVPService : IRSVP
{
public RSVPService()
{
}
public bool Attending()
{
return true;
}
public bool NotAttending()
{
return true;
}
}
}
I'd like to self-host in a console application like so:
class Program
{
public static void Main()
{
// Create a ServiceHost
using (ServiceHost serviceHost = new ServiceHost(typeof(RSVPService)))
{
// Open the ServiceHost to create listeners
// and start listening for messages.
serviceHost.Open();
// The service can now be accessed.
Console.WriteLine("The service is ready.");
Console.WriteLine("Press <ENTER> to terminate service.");
Console.WriteLine();
Console.ReadLine();
}
}
}
So I'm using this app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6"/>
</startup>
<system.serviceModel>
<services>
<service name="Vert.Host.VertService.RSVPService">
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080/Vert" />
</baseAddresses>
</host>
<endpoint
address="/RSVP"
binding="basicHttpBinding"
contract="Vert.Host.VertService.IRSVP" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpsGetEnabled="true" httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
As I understand it, this setup would leave me with http://localhost:8080/Vert/RSVP/Attending as a valid REST URI to call from an arbitrary HTTPClient, but the call is hanging indefinitely or coming back with a 0 No Response (I'm using Advanced REST client)
What am I missing?
You are RIGHT in all of your setup...right up to the point where you stopped typing code and started telling me what you have. :)
What you've created is a std WCF service and you can get to it using a Service proxy or a ChannelFactory, but it will communicate with you as-is using SOAP.
You need this tutorial to turn this webservice into a RESTFUL service giving back Json/pox.
This question already has answers here:
HTTP could not register URL http://+:10001/. Your process does not have access rights to this namespace
(3 answers)
Closed 8 years ago.
I have written a simple WCF code. and want to host it . but when I run it shows me an exception
HTTP could not register URL http://+:8080/. Your process does not have access rights to this namespace
I have a class library project, in which I have written my WCF code. then I added App.config file and write some codes
App.config code :
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />
</startup>
<system.serviceModel>
<services>
<service name="DEMO1.HelloService" behaviorConfiguration="mexBehavior">
<endpoint address="HelloService" binding="basicHttpBinding" contract="DEMO1.IHelloService"></endpoint>
<endpoint address="HelloService" binding="netTcpBinding" contract="DEMO1.IHelloService"></endpoint>
<endpoint address="Mex" binding="mexHttpBinding" contract="IMetadataExchange"></endpoint>
<host>
<baseAddresses>
<add baseAddress="http://localhost:8080"/>
<add baseAddress="net.tcp://localhost:8090"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="mexBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
this is my Interface
[ServiceContract]
public interface IHelloService
{
[OperationContract]
string GetMessage(string name);
}
and i implemented this
public class HelloService : IHelloService
{
public string GetMessage(string name)
{
return "Name : " + name;
}
}
then I added another console project to that solution and
class Program
{
static void Main()
{
using(ServiceHost host=new ServiceHost(typeof(DEMO1.HelloService)))
{
try
{
host.Open();
Console.WriteLine("Started");
}catch(Exception ex)
{
Console.WriteLine(ex.Message);
}
Console.ReadLine();
}
}
}
My hierarchy
This problem come when you can change manually baseaddress. So wcf service doesn't find the service location where service is located so if you can change your service baseaddress port then you can change only port number and shouldn't change all address. If you changed all address then address doesn't find service location and service has been giving error.
Any service which is hosted via HTTP.SYS (which includes WCF) requires permission to the namespace you are attempting to use (http://localhost:8080, in your case).
Either run as administrator, or add an urlacl which allows your process access to the namespace.
In an administrator command prompt:
netsh http add urlacl url=http://+:8080/ user=BUILTIN\Users
I have a windows service that hosts a WCF service. I have a client that consumes the service. The client does not recognize the ServiceReference1 even though I added it as a Service Reference.
I have been trying to fix this all day - I have read the code multiple times - I have used WCF before with no issues (in the same manner as this).
Interface
namespace ValidStateService
{
[ServiceContract]
public interface IValidStateWCFService
{
// Add two numbers
[OperationContract(IsOneWay = false)]
int AddNumbers(int numA, int numB);
}
}
Implementation of the Interface
namespace ValidStateService
{
public class ValidStateWCFService : IValidStateWCFService
{
// Call the agent to add two numbers and return the result
public int AddNumbers(int numA, int numB)
{
try
{
return 20; // Always return 20 for testing
}
catch (Exception ex)
{
return -1;
}
}
}
}
Windows Service App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="ValidStateService.ValidStateWCFService">
<endpoint address="" binding="basicHttpBinding" contract="ValidStateService.IValidStateWCFService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/ValidStateService/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
Now on a Winform i have added reference to the service but servicereference1 is not recognized.
Code to add two numbers
*Winform App.config*
private void buttonGetDataFromAgent_Click(object sender, EventArgs e)
{
try
{
InstanceContext context = new InstanceContext(this);
ServiceReference1. // Winform App does not recognize this i.e no intellisense
??????
Client App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<system.serviceModel>
<client>
<endpoint address="http://localhost:8732/ValidStateService/"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IValidStateWCFService"
contract="ServiceReference1.IValidStateWCFService" name="BasicHttpBinding_IValidStateWCFService" />
</client>
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IValidStateWCFService" />
</basicHttpBinding>
</bindings>
</system.serviceModel>
</configuration>
In my book, it wants me to expose two endpoints using two bindings: WsHttpBinding & NetTCPBinding and host the service in a host application.
I use the following code in C# to try to connect to my service:
Uri BaseAddress = new Uri("http://localhost:5640/SService.svc");
Host = new ServiceHost(typeof(SServiceClient), BaseAddress);
ServiceMetadataBehavior Behaviour = new ServiceMetadataBehavior();
Behaviour.HttpGetEnabled = true;
Behaviour.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
Host.Description.Behaviors.Add(Behaviour);
Host.Open();
On the service side I have:
[ServiceContract]
public interface IService...
public class SService : IService....
Then in my Config file I have:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="Behavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="WsHttpBehaviour" name="SService.SService">
<endpoint
address=""
binding="wsHttpBinding"
bindingConfiguration="WsHttpBindingConfig"
contract="SService.IService" />
<endpoint
address=""
binding="netTcpBinding"
bindingConfiguration="NetTCPBindingConfig"
contract="SService.IService" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:5640/SService.svc" />
<add baseAddress="net.tcp://localhost:5641/SService.svc" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
But when I try to add service reference to my host application, it says unable to download metadata from the address. I don't understand what is wrong and the teacher never taught this.. I decided to go ahead and look it up and learn ahead of time. I used the Editor to create the WebConfig shown above.
Can anyone point me in the right direction? I added Metadata behaviour through the editor and I set the HttpGetEnabled to true.
I can find a few issues with your code that can cause this issue:
Host = new ServiceHost(typeof(SServiceClient), BaseAddress). Pass here typeof(SService.SService) instead of typeof(SServiceClient). Change like this:
Host = new ServiceHost(typeof(SService.SService))
<service behaviorConfiguration="WsHttpBehaviour". I guess this should be "Behavior" as you have defined that. Since you have metadata enabled in config, you may remove the lines of code which add a ServiceMetadataBehavior to your servicehost.
Here is a sample that you can use for reference:
<system.serviceModel>
<services>
<service name="ConsoleApplication1.Service">
<endpoint address="" binding="wsHttpBinding" contract="ConsoleApplication1.IService" />
<endpoint address="" binding="netTcpBinding" contract="ConsoleApplication1.IService" />
<host>
<baseAddresses>
<add baseAddress="http://<machinename>:5640/SService.svc" />
<add baseAddress="net.tcp://<machinename>:5641/SService.svc" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="False"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ServiceHost host = new ServiceHost(typeof(Service));
host.Open();
Console.ReadLine();
}
}
[ServiceContract]
public interface IService
{
[OperationContract]
string DoWork();
}
public class Service : IService
{
public string DoWork()
{
return "Hello world";
}
}
}
Metadata will be automatically available at the http baseaddress defined in the config.
I'm running a WCF service hosted in a WPF app, and am trying to raise an event in the host when a message is sent to the service, but am having great difficulty.
My code is as follows:
Service -
namespace BatService
{
[ServiceContract]
public interface IBatServ
{
[OperationContract]
void UseGadget(string name);
}
[ServiceBehavior(InstanceContextMode=InstanceContextMode.Single)]
public class BatServ : IBatServ
{
public void UseGadget(string name)
{
OnUsedGadget(name);
}
public static event EventHandler<BatArgs> UsedGadget;
public static void OnUsedGadget(string name)
{
if (UsedGadget != null)
UsedGadget(null, new BatArgs() { BatGadget = name });
}
}
public class BatArgs : EventArgs
{
public string BatGadget;
}
}
Host -
namespace BatHostWPF
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
ServiceHost host = new ServiceHost(typeof(BatServ));
BatServ.UsedGadget += new EventHandler<BatArgs>(BatServ_UsedGadget);
host.Open();
}
void BatServ_UsedGadget(object sender, BatArgs e)
{
MessageBox.Show(e.BatGadget + " was used!");
}
}
}
Service App.config -
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.web>
<compilation debug="true" />
</system.web>
<!-- When deploying the service library project, the content of the config file must be added to the host's
app.config file. System.Configuration does not support config files for libraries. -->
<system.serviceModel>
<services>
<service name="BatService.BatServ">
<endpoint address="" binding="wsHttpBinding" contract="BatService.IBatServ">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/BatService/Service1/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<!-- To avoid disclosing metadata information,
set the value below to false and remove the metadata endpoint above 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>
</behaviors>
</system.serviceModel>
</configuration>
Host's App.config -
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewBehavior0">
<serviceMetadata httpGetEnabled="true" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="NewBehavior0" name="BatService.BatServ">
<clear />
<endpoint address="net.pipe://localhost/battserv" binding="netNamedPipeBinding"
bindingConfiguration="" contract="BatService.IBatServ" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8888/batserv" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
As you can probably guess, I'm expecting to see a MessageBox when I call UseGadget() from a client. Whenever I try to test it out with VS's WcfTestClient.exe, nothing seems to happen at all. Where am I going wrong?
It turns out my endpoints weren't configured correctly. This page helped me solve my problems - http://www.switchonthecode.com/tutorials/wcf-tutorial-basic-interprocess-communication
I'm not sure if you can "raise events" in wcf, but you can create duplex communication architectures. The netTCPBinding, netNamedPipBinding and wsDualHttpBinding support this functionality.
Here is a video demonstration how to do callbacks with the wsDualHttpBinding
http://www.youtube.com/watch?v=NO2JsLrP75E
I've never done this with netNamedPipeBinding, but i assume the procedure is similar.
Remember to update your app.config files and service references when you're done with your implementation.
Hope that helps!