Error creating wcf .svc service - c#

I have created following svc service given below-
public class NewsRest : INewsRest
{
public string getHomePageNews1()
{
DataTable dt = new DataTable();
try
{
dt = new ManageNews().getHomePageNews1();
string json = new Utills().dataTableToJsonString(dt);
json = json.Replace("task-photo-846b6edc-3b2e-4ff6-ba10-734d64143c1d.png", "task-photo-8d5feed4-d6a1-4ae9-9a72-387ccfda1dc5.jpg");
return json;
}
catch (Exception ex)
{
return "{}";
}
}
}
[ServiceContract]
public interface INewsRest
{
[OperationContract]
[WebGet(ResponseFormat=WebMessageFormat.Json)]
string getHomePageNews1();
}
and web config configuration is-
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="NewsRestBehavior">
<serviceMetadata httpGetEnabled="true" policyVersion="Policy15" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="UPNews.WebServices.NewsRest" behaviorConfiguration="NewsRestBehavior">
<endpoint address=""
binding="basicHttpBinding"
contract="UPNews.WebServices.INewsRest" />
<endpoint address="mex" binding="mexHttpBinding" name="MetadataEndpoint"
contract="IMetadataExchange" />
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
but when I am testing this service on WCF test client it gives following error.-
Error: Cannot obtain Metadata from http://localhost:57358/WebServices/NewsRest.svc/getHomePageNews1
If this is a Windows (R) Communication Foundation service to which you have access,
please check that you have enabled metadata publishing at the specified address.
For help enabling metadata publishing,
please refer to the MSDN documentation at http://go.microsoft.com/fwlink/?LinkId=65455.WS-Metadata Exchange Error
URI: http://localhost:57358/WebServices/NewsRest.svc/getHomePageNews1 Metadata contains a reference that cannot be resolved:
'http://localhost:57358/WebServices/NewsRest.svc/getHomePageNews1'. Content Type application/soap+xml;
charset=utf-8 was not supported by service http://localhost:57358/WebServices/NewsRest.svc/getHomePageNews1.
The client and service bindings may be mismatched. The remote server returned an error:
(415) Unsupported Media Type.HTTP GET Error URI: http://localhost:57358/WebServices/NewsRest.svc/getHomePageNews1 There was an error downloading
'http://localhost:57358/WebServices/NewsRest.svc/getHomePageNews1'. The request failed with HTTP status 400: Bad Request.
Please tell whats wrong happening here.

You must add to your server configuration to allow MEX (meta data exchange) so that the client can retrieve the generated WSDL. Something like this:
<service name="UPNews.WebServices.NewsRest" behaviorConfiguration="NewsRestBehavior">
<endpoint address=""
binding="basicHttpBinding"
contract="UPNews.WebServices.INewsRest" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>

Related

WCF Could not find endpoint element and contract

I'm trying to create a WCF and consume it from another C# class.
I think I'm doing everything right but i Get the error message says Contract name is not familiar.
This is my Web.config code:
<system.serviceModel>
<services>
<service behaviorConfiguration="PNMSoft.Sequence.Invoices.InvoicesBehavior" name="PNMSoft.Sequence.Invoices.Invoices">
<endpoint name="InvoicesEndPoint" address="" binding="wsHttpBinding" contract="PNMSoft.Sequence.Invoices.IInvoices">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="PNMSoft.Sequence.Invoices.InvoicesBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
This is my interface code:
namespace PNMSoft.Sequence.Invoices
{
[ServiceContract]
public interface IInvoices
{
[OperationContract]
void SendInvoicesToCustomer(int wfid, int userID, int type, string mail, DateTime startDate, DateTime endDate, string clientCode);
}
}
This is my code when I try to activate a function from the service:
InvoicesClient client = new InvoicesClient("InvoicesEndPoint");
client.SendInvoicesToCustomer(IWFID, userID2, type2, Mail, DateFrom2, DateTo2, clientCode2);
On the first row I get this error message: "Could not find endpoint element with name 'InvoicesEndPoint' and contract 'Invoices.IInvoices' 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.".
This is the config file on my cient's side:
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="InvoicesEndPoint" />
</wsHttpBinding>
</bindings>
<client>
<endpoint address="" binding="wsHttpBinding" bindingConfiguration="InvoicesEndPoint" contract="Invoices.IInvoices" name="InvoicesEndPoint">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
</client>
</system.serviceModel>
I guess I need to add something to the config file in my client's side. What should I add?
Thank you in advance!
I solved it, I just had to add the endpoint to the client side's config.

WCF error - Can't get service endpoint up

I started working with WCF recently, and I'm having a problem that I just don't have a clue how to solve.
I start a WCF Service using Service Host, but when I use the URI in a browser it doesn't show the contract of the service, and it gives an exception when I try to connect to it using a ChannelFactory.
I created the project in Visual Studio 2017, and didn't do anything to the config file, excpet changing the base address. Both the service interface and implementation are in the root project "folder", and I've tried disabling the firewall and even my antivirus, but nothing seems to work.
App.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="TaskExecutor.Exec">
<endpoint address="" binding="basicHttpBinding" contract="TaskExecutor.IExec">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/TaskExecutor/Exec/" />
</baseAddresses>
</host>
</service>
</services>
</system.serviceModel>
</configuration>
Service interface:
namespace TaskExecutor
{
[ServiceContract]
public interface IExec
{
[OperationContract]
void DoWork();
}
}
Service implementation:
namespace TaskExecutor
{
public class Exec : IExec
{
public void DoWork()
{
Console.WriteLine("Doing work.");
}
}
}
Program launching the service:
using (ServiceHost host = new ServiceHost(typeof(Exec)))
{
host.Open();
Console.WriteLine("exec service started at {0}", host.BaseAddresses[0]);
}
Console.WriteLine("Press any key to end...");
Console.ReadLine();
After launching the program display the message:
exec service started at http://localhost:8001/TaskExecutor/Exec/
Press any key to end...
The service client code is the following:
EndpointAddress endpoint = new EndpointAddress("http://localhost:8001/TaskExecutor/Exec/");
BasicHttpBinding binding = new BasicHttpBinding();
ChannelFactory<IExec> channelFactory = new ChannelFactory<IExec>(binding, endpoint);
IExec proxy = channelFactory.CreateChannel();
proxy.DoWork();
And it gives the exception:
System.ServiceModel.EndpointNotFoundException occurred
HResult=0x80131501
Message=There was no endpoint listening at http://localhost:8001/TaskExecutor/Exec/ that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
Inner Exception 1:
WebException: Unable to connect to the remote server
Inner Exception 2:
SocketException: No connection could be made because the target machine actively refused it
I seriously don't know what to do, and any help would be amazing.
Thank you very much!
You have exposed metadata but didnt bind it to the service.Here's how you have to.
<behaviors>
<serviceBehaviors>
<behavior name="metadadiscovery">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
Now bind the behavior to the service.
<services>
<service name="TaskExecutor.Exec" behaviorConfiguration="metadadiscovery">
<endpoint address="" binding="basicHttpBinding" contract="TaskExecutor.IExec">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8001/TaskExecutor/Exec/" />
</baseAddresses>
</host>
</service>
</services>
Now when you type the address in the browser you should be able to see wsdl.
http://localhost:8001/TaskExecutor/Exec/
So I figured out what was wrong, It was the code starting the service. instead of what I have It should be:
using (ServiceHost host = new ServiceHost(typeof(Exec)))
{
host.Open();
Console.WriteLine("exec service started at {0}", host.BaseAddresses[0]);
Console.WriteLine("Press any key to end...");
Console.ReadLine();
}

Consuming WCF Data Service From Metro Application

I'm develping a Metro application, and a WCF Data service hosted in a Windows Service using TCP. The Metro App will use this WCF service to consume data. I get the Windows Service host running so the WCF Service is available.
The problem is that running Metro App to test this configuration in my develop box I get System.ServiceModel.Security.SecurityNegotiationException. Inner exception is System.Security.AuthenticationException: "A call to SSPI failed, see inner exception.") , which in turn have this other inner exception too: System.ComponentModel.Win32Exception: "No credentials are available in the security package".
My Test environment:
Service contract:
[ServiceContract]
public interface IWCFDataService
{
[OperationContract]
Device GetDevice(string ip);
}
[DataContract]
public class Device
{
[DataMember]
public string Name { get; set; }
}
Service code:
public class WCFDataService : IWCFDataService
{
public Device GetDevice(string ip)
{
return new Device { Name = "Sample device" };
}
}
WCF Service configuration:
<system.serviceModel>
<services>
<service name="Apollo.DataService.WCFDataService">
<endpoint address="" binding="netTcpBinding" bindingConfiguration=""
contract="Apollo.DataService.IWCFDataService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexTcpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="net.tcp://localhost:8523/WCFDataService" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
Metro Application consuming WCF Data Service:
var client = new DataService.WCFDataServiceClient();
var peripheral = await client.GetDevice("127.0.0.1"); <-- Throw Exception
UPDATE:
If I consume the WCF service from a Windows Form application every thing work fine, so the problem is something with Metro Application.

How to publish wcf service metadata when the wcf is self-hosted?

I googled for 2 days and tried almost everything, but I still can't get this stuff working.
I have 2 WCF services. I use self-hosting, not IIS(for some reasons IIS isn't working for me). One is duplex, another is standart. Here are their contracts:
Duplex:
[ServiceContract(SessionMode = SessionMode.Required, CallbackContract = typeof(IServiceCallback))]
public interface IClientService
{
[OperationContract(IsOneWay = true)]
void SolveTask(string pipelineName, string data);
[OperationContract(IsOneWay = true)]
void GenerateTask(List<GeneratorMethod> parameters);
[OperationContract]
bool Ping();
}
public interface IServiceCallback
{
[OperationContract(IsOneWay = true)]
void SendResult(SampleAnswer[] answers);
[OperationContract(IsOneWay = true)]
void RequestGeneratorParameters();
[OperationContract(IsOneWay = true)]
void SendGenerationResult(string text);
}
Classic:
[ServiceContract]
public interface IServerManagementService
{
[OperationContract]
[FaultContract(typeof(XmlError))]
[FaultContract(typeof(UnknownError))]
[FaultContract(typeof(InitializationError))]
void InitializeServer();
[OperationContract]
void StartServer();
[OperationContract]
void StopServer();
[OperationContract]
void RestartServer();
}
I have following config:
<configuration>
<services>
<service behaviorConfiguration="Service" name="LinProgWebServer.ClientService">
<endpoint address="net.tcp://localhost:8078/LinProgWebServer/ClientService"
binding="netTcpBinding" bindingConfiguration="netTcpEventBinding"
contract="LinProgWebServer.IClientService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8731/Design_Time_Addresses/LinProgWebServer/ClientService" />
</baseAddresses>
</host>
</service>
<!--сервис управления сервером-->
<!--<service behaviorConfiguration="Service" name="LinProgWebServer.ServerManagementService">
<endpoint address="net.tcp://localhost:8079/LinProgWebServer/ManagementService"
binding="netTcpBinding" bindingConfiguration="netTcpEventBinding"
contract="LinProgWebServer.IServerManagementService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" bindingConfiguration=""
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/LinProgWebServer/ManagementService" />
</baseAddresses>
</host>
</service>-->
</services>
<behaviors>
<serviceBehaviors>
<behavior name="Service">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
Now i have a big trouble: I CAN find classic service via add service reference and CANNOT find duplex service. I tried netstat and it says that both services are listening on their ports. What am i doing wrong?
Here is exception i get:
There was an error downloading 'http://localhost:8731/Design_Time_Addresses/LinProgWebServer/ClientService/_vti_bin/ListData.svc/$metadata'.
The request failed with HTTP status 405: Method Not Allowed.
Metadata contains a reference that cannot be resolved: 'http://localhost:8731/Design_Time_Addresses/LinProgWebServer/ClientService'.
There was no endpoint listening at http://localhost:8731/Design_Time_Addresses/LinProgWebServer/ClientService that could accept the message. This is often caused by an incorrect address or SOAP action. See InnerException, if present, for more details.
The remote server returned an error: (404) Not Found.
If the service is defined in the current solution, try building the solution and adding the service reference again.
I'll be grateful for any help.
Make sure you .svc file has the correct name of the class you added in your Service Reference
if you added a service called 'Foo' it should look like:
<%# ServiceHost Language="C#" Debug="true" Service="Foo" CodeBehind="Foo.svc.cs" %
Also, make sure you have
In your web.config
I'm not sure whether this is going to be usefull. All my configs and services were fine, but i had a mistace in types, which were involved in work of wcf services, i haven't marked one of the types as datacontract. U'm very surprised why service started without erros and showed me such not informative messages. Also, thanks Yuval for reminding about InstanceContextMode attribute.
Add this to the area where you setup you set up your endpoint and reference your service classes:
//config service metadata
using System.ServiceModel;
using System.ServiceModel.Channels;
using System.ServiceModel.Description;
ServiceMetadataBehavior mb = new ServiceMetadataBehavior();
ServiceHost.Description.Behaviors.Add(mb);
if (bUseSSL) {
mb.HttpsGetEnabled = true;
mb.HttpGetEnabled = false;
ServiceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpsBinding(), "mex");
} else {
mb.HttpsGetEnabled = false;
mb.HttpGetEnabled = true;
ServiceHost.AddServiceEndpoint(ServiceMetadataBehavior.MexContractName, MetadataExchangeBindings.CreateMexHttpBinding(), "mex");
}
You should not be using the same URL that you were using to host it in the dev environment. Localhost means nothing outside of the machine, anyway. Use the IP address of the machine and also -- you might have to use netsh to open the port on that machine.

There was no channel actively listening

maybe you can help me set up my WCF service.
First, here is my config file:
<system.serviceModel>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
<bindings />
<services>
<service name="AuthenticatorService.Authenticator">
<endpoint address="auth" binding="basicHttpBinding" bindingConfiguration=""
name="AuthEndpoint" contract="AuthInterface.IAuthenticator" />
<endpoint address="mex" binding="mexHttpBinding" name="MetadataEndpoint"
contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" />
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
</behaviors>
And this is how I call my service from c#:
//This creates a link to the WCF service using basicHttpBingind
httpFactory = new ChannelFactory<IAuthenticator>(new BasicHttpBinding(), new EndpointAddress("http://myUrl/auth.svc"));
httpProxy = httpFactory.CreateChannel();
It worked fine when I was doing this on localhost but now it keeps telling me no endpoint was found.
Also, the server generated the following error:
System.ServiceModel.EndpointNotFoundException: There was no channel actively listening at 'http://myURL/auth.svc/$metadata'. This is often caused by an incorrect address URI. Ensure that the address to which the message is sent matches an address on which a service is listening.
Im really confused, I have no idea why this is happening. Do I need to create another service file for the metadata exchange?
Do I need to set a baseAddress?
Thanks
How did you deploy the service? I assume the service is running in IIS on your box - did you try hitting the service URL (http://myUrl/auth.svc) in a browser to see if it is indeed up?

Categories