Declaration of function with json configuration
namespace Wcf_Test2
{
[ServiceContract]
public interface IMyService
{
[OperationContract]
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare,
UriTemplate = "dowork")]
string DoWork();
}
}
It is function that returns string value.
public class MyService : IMyService
{
public string DoWork()
{
string str = "HI, Its's DOWork Running";
return str;
}
}
This is Web.config file which I use.
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<system.web>
<compilation debug="false" targetFramework="4.5" />
<httpRuntime targetFramework="4.5"/>
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="Default" name="Wcf_Test2.MyService">
<endpoint address="" binding="basicHttpBinding"
contract="Wcf_Test2.IMyService" />
<endpoint address="mex" binding="mexHttpBinding"
contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost/Wcf_Test2/MyService.svc" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="Default">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
I am calling this hosted web service as http://localhost/Wcf_Test2/MyService.svc/dowork
but it's replying this
The error is this which this picture is showing me.
Related
I have to make a WCF service in visual studio 2015 MVC and it needs to be accesible only by SSL certificate, I´m using IIS (in Windows10) and trying to limit SSL for only this service (stored in a specifc folder).
When I change IIS option of this folder to "require SSL" the service said that "the http request is unauthorized with client authentication scheme 'anonymous'"
I changed web.config too, maybe something is bad there, the service´s name is EncServiceSSL
How can I do it?
WEB.config:
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="ServiceCredentialsBehavior">
<serviceMetadata httpGetEnabled="false" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
<serviceAuthorization serviceAuthorizationManagerType="MyNamespace.ClientCertificateValidator, MyAssembly">
<authorizationPolicies>
<add policyType="MyNamespace.AdamAuthorizationPolicy, MyAssembly" />
</authorizationPolicies>
</serviceAuthorization>
<serviceCredentials>
<serviceCertificate findValue="1234xx" x509FindType="FindBySubjectName" storeLocation="LocalMachine" storeName="Root"/>
<clientCertificate>
<authentication certificateValidationMode="PeerTrust" trustedStoreLocation="LocalMachine" />
</clientCertificate>
</serviceCredentials>
<useRequestHeadersForMetadataAddress>
<defaultPorts>
<add scheme="https" port="443"/>
</defaultPorts>
</useRequestHeadersForMetadataAddress>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceCredentialsBehavior" name="System.Web.ApplicationServices.AuthenticationService">
<endpoint address="http://localhost:8111/ServicesSSL/EncServiceSSL"
binding="wsHttpBinding"
bindingConfiguration="QuoteService"
name="SecuredByClientCertificate"
contract="encelogistica.QuoteServiceReference.QuoteServiceSoapChannel" />
</service>
</services>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
<bindings>
<basicHttpBinding>
<binding name="QuoteService">
<security mode="Transport">
<transport clientCredentialType="Windows" />
</security>
</binding>
<binding name="QuoteServiceSoap" />
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8111/ServicesSSL/EncServiceSSL.asmx"
binding="basicHttpBinding" bindingConfiguration="QuoteServiceSoap"
contract="QuoteServiceReference.QuoteServiceSoap" name="QuoteServiceSoap" />
</client>
</system.serviceModel>
</configuration>
Currently our services use Message level security, but as we are using Soap UI for testing. After many hours, we discovered that Soap UI is not good with Message level security, something to do with java core I believe.
Have tried to unravel the web.config to use Transport level security, which I believe is supported by Soap UI, but as soon as I change the security mode to Transport I get the following error:
Could not find a base address that matches scheme https for the endpoint with binding WSHttpBinding. Registered base address schemes are [http].
Can someone have a look at the config as I am sure that I have missed something.
<system.serviceModel>
<services>
<service name="MyService"
behaviorConfiguration="ServiceBehavior">
<endpoint address=""
binding="wsHttpBinding"
bindingConfiguration="ServiceWsHttpBinding"
name="ClaimServiceHttpEndpoint"
contract="IMyService" />
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange" />
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="ServiceWsHttpBinding"
maxBufferPoolSize="2147483647"
maxReceivedMessageSize="2147483647">
<readerQuotas maxDepth="2000000"
maxStringContentLength="2147483647"
maxArrayLength="2147483647"
maxBytesPerRead="2147483647"
maxNameTableCharCount="2147483647" />
<security mode="Message">
<message clientCredentialType="UserName"
negotiateServiceCredential="false"
establishSecurityContext="false" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<behaviors>
<endpointBehaviors>
<behavior name="ClientEndpointBehavior">
<clientCredentials>
<serviceCertificate>
<authentication certificateValidationMode="None" />
</serviceCertificate>
</clientCredentials>
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="false" />
<serviceDebug includeExceptionDetailInFaults="true" />
<serviceCredentials>
<serviceCertificate findValue="CN=test.service.com"
storeLocation="LocalMachine"
storeName="My"
x509FindType="FindBySubjectDistinguishedName" />
<userNameAuthentication userNamePasswordValidationMode="Custom"
customUserNamePasswordValidatorType="MyObject.UserAuthentication, MyObject.Utils" />
</serviceCredentials>
<serviceAuthorization principalPermissionMode="Custom">
<authorizationPolicies>
<add policyType="MyObject.Utils.AuthPolicy, Myobject.Utils" />
</authorizationPolicies>
</serviceAuthorization>
</behavior>
<behavior name="">
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<protocolMapping>
<add binding="basicHttpsBinding" scheme="https" />
</protocolMapping>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true" />
I created a WCF service and hosted it on my IIS server. Then, I needed to edit the schemaLocation. I followed this post.
When I add "?wsdl" to the url, I get an empty page. If I try to use the WSDL from SOAPui to test it, I get this "Error loading [http://xx.xxx.xx.xx:8095/CardServiceLib.CardService.svc/service?wsdl]: org.apache.xmlbeans.XmlException: org.apache.xmlbeans.XmlException: error: Unexpected end of file after nul"
If I delete the HTTPGetUrl, the WSDL is correct but the schemaLocation isn't what I want. With the mod I made with the linked post, the schemaLocation is perfect but the WSDL is empty... why?
This works, but not as I want (generates WSDL but with wrong links):
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
and this not (doesn't generate WSDL --> blank page!!!):
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" httpGetUrl="http://xx.xx.xx.xx.:8095/MyService.svc/endpoint"/>
This is my web.config on IIS server 7
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<connectionStrings>
<add name="connectionString" connectionString="server=xxxxx;database=xxxxx;uid=xxxxx;pwd=xxxxx;" providerName="System.Data.SqlClient" />
</connectionStrings>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
<add key="userName" value="admin" />
<add key="password" value="xxxxx" />
<add key="ClientSettingsProvider.ServiceUri" value="" />
<add key="SecurityKey" value="xxxxxxxxxx" />
</appSettings>
<system.web>
<compilation debug="true" />
<membership defaultProvider="ClientAuthenticationMembershipProvider">
<providers>
<add name="ClientAuthenticationMembershipProvider" type="System.Web.ClientServices.Providers.ClientFormsAuthenticationMembershipProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" />
</providers>
</membership>
<!--<roleManager defaultProvider="ClientRoleProvider" enabled="true">
<providers>
<add name="ClientRoleProvider" type="System.Web.ClientServices.Providers.ClientRoleProvider, System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" serviceUri="" cacheTimeout="86400" />
</providers>
</roleManager>-->
</system.web>
<system.serviceModel>
<standardEndpoints>
<webHttpEndpoint>
<!-- the "" standard endpoint is used for auto creating a web endpoint. -->
<standardEndpoint name="" helpEnabled="true" automaticFormatSelectionEnabled="true" />
</webHttpEndpoint>
</standardEndpoints>
<bindings>
<basicHttpBinding>
<binding name="SecurityByTransport" maxBufferPoolSize="2147483647" maxBufferSize="2147483647" maxReceivedMessageSize="2147483647" messageEncoding="Text">
<readerQuotas maxDepth="2000000" maxStringContentLength="2147483647" maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxNameTableCharCount="2147483647" />
<security mode="TransportCredentialOnly">
<transport clientCredentialType="Basic" />
<!--<message clientCredentialType="UserName"/>-->
</security>
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="CardServiceLib.CardService" behaviorConfiguration="customBehavior">
<endpoint address="" binding="basicHttpBinding" bindingConfiguration="SecurityByTransport" name="base" contract="CardServiceLib.ICardService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" name="mex" contract="IMetadataExchange" />
<!--<endpoint address="web" behaviorConfiguration="webHttp" binding="webHttpBinding"
bindingConfiguration="" name="web" contract="calc.ICalcService">
<identity>
<dns value="localhost" />httpGetUrl="http://77.108.40.77:8095/CardServiceLib.CardService.svc/service"
</identity>
</endpoint>-->
<host>
<baseAddresses>
<add baseAddress="https://xx.xx.xx.xx:8095/MyService/" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior name="webHttp">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
<serviceDebug includeExceptionDetailInFaults="False" />
</behavior>
<behavior name="customBehavior">
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" httpGetUrl="http://xx.xx.xx.xx:8095/MyService.Service.svc/service"/>
<serviceDebug includeExceptionDetailInFaults="True" />
<serviceCredentials>
<userNameAuthentication userNamePasswordValidationMode="Custom" customUserNamePasswordValidatorType="xx,xx" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<!--<protocolMapping>
<add binding="webHttpBinding" scheme="http" />
</protocolMapping>-->
</system.serviceModel>
<system.webServer>
<directoryBrowse enabled="false" />
</system.webServer>
<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </startup></configuration>
SOLVED!! I added this
address="http://192.168.1.2/Demo.Service/MultiEndPointsService.svc/basic"
in my endpoint tag, like written in this link.
I suspect the issue is related to the authentication required to access the WSDL.
When attempting to access the metadata URL directly, the web site indicates the following:
“Authentication Required”
“The server http://...:8095 requires a username and password.”
You either need to provide the proper credentials or relax the permissions on the WSDL.
I've spent hours trying to figure out this particular error and have had no luck.
I keep getting an error reading like I'm missing an endpoint and I'm new to WCF web Services so I'm not sure what direction to look. Anyway, The error reads.
Could not find default endpoint element that references contract 'ServiceReference1.ServiceContract' 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.
I create the object like this and then add a method.
ServiceReference1.ServiceContractClient test = new ServiceReference1.ServiceContractClient();
var connecting = test.Connect();
I have endpoints in my web.config file of the WcfProject. Here's my web.config
<?xml version="1.0"?>
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
<sessionState cookieless="false" mode="InProc"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="X-UA-Compatible" value="IE=edge" />
<add name="Access-Control-Allow-Origin" value="*" />
</customHeaders>
</httpProtocol>
</system.webServer>
<system.serviceModel>
<bindings>
<webHttpBinding>
<binding name="RestBinding"></binding>
</webHttpBinding>
</bindings>
<services>
<service behaviorConfiguration="ServiceBehavior" name="WcfRestService1.Service">
<endpoint name="ServiceBinding" contract="WcfRestService1.IService" binding="webHttpBinding" bindingConfiguration="RestBinding" behaviorConfiguration="RestBehavior" />
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint address="basic" binding="wsHttpBinding" bindingConfiguration="" contract="WcfRestService1.IService" />
<host>
<baseAddresses>
<add baseAddress="http://servername/WcfRestService1/Service.svc" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="true" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="RestBehavior">
<webHttp helpEnabled="true" />
</behavior>
</endpointBehaviors>
</behaviors>
<client>
<endpoint name="Default"
address="http://servername/WcfRestService1/Service.svc"
binding="webHttpBinding"
bindingConfiguration="RestBinding"
behaviorConfiguration="RestBehavior"
contract="WcfRestService1.IService" />
</client>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" />
</system.serviceModel>
</configuration>
I'm not sure what i need.
Thanks for any help.
You can not consume REST service using Service Proxy generated either by using Visual studio add service reference feature or using svcutil.exe. You need to use WebClient, HttpWebRequest or any other third party library capable of making http calls.
I am creating a REST API in .net with Post method as I want to extend getting data from the client ( Note I dont want to use GET method).
Here is my simple REST API which returns error " Method not allowed" . WHat is missing ??
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "/Test/{id}")]
void Test(string id);
Url call is http://localhost/RestTestWebService.svc/json/Test/abs. This call returns Method not allowed error.
Web.config file
<system.web>
<compilation debug="true" targetFramework="4.0" />
<!--<authentication mode="Forms">
<forms loginUrl="~/Account/Login.aspx" timeout="2880" />
</authentication>-->
<membership>
<providers>
<clear />
<add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
</providers>
</membership>
<profile>
<providers>
<clear />
<add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/" />
</providers>
</profile>
<roleManager enabled="false">
<providers>
<clear />
<add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
<add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
</providers>
</roleManager>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
<system.serviceModel>
<behaviors>
<endpointBehaviors>
<behavior name="WebHttpBehavior">
<webHttp />
</behavior>
</endpointBehaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
<behavior name="RestTestWebServiceBehaviors">
<serviceMetadata httpGetEnabled="true" />
<serviceDebug includeExceptionDetailInFaults="false" />
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service name="RestTest.RestTestWebService" behaviorConfiguration="RestTestWebServiceBehaviors" >
<endpoint name="json" address="json" binding="webHttpBinding" behaviorConfiguration="WebHttpBehavior" contract="RestTest.IRestTestWebService"/>
<!--<endpoint name="json" address="" binding="basicHttpBinding" contract="RestTest.IRestTestWebService"/>-->
</service>
</services>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
</system.serviceModel>
</configuration>
UriTemplate must be "/Test". Your operation contract should look like:
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json, UriTemplate = "/Test")]
void Test(string id);
Your URL call should be:
var myTestUrl = 'http://localhost/RestTestWebService.svc/json/Test'
You should send 'id' in the 'data' parameter of the ajax call:
$.ajax({
url: myTestUrl,
contentType: "application/json; charset=utf-8",
type: 'POST',
data: JSON.stringify({ 'id': 12345}),
dataType: 'json'
}).done(function (response) {
console.log('done');
});
(Also, address="json" on service endpoint is not necessary, you can simply have adress="" and url = http://localhost/RestTestWebService.svc/Test)
UPDATE Sending the id in the url isn't incorrect, it is in my opinion not like the HTTP protocol