WCF Hosting with IP Address - wcf

I have created a WCF Service which return JSON data.
here is my code:
namespace AppServices
{
[ServiceContract]
public interface Service1
{
[OperationContract]
[WebInvoke(Method = "GET", UriTemplate = "/GetCities", BodyStyle = WebMessageBodyStyle.WrappedRequest,
RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
List<City> GetCityCode();
}
[DataContract]
public class City
{
[DataMember]
public string CityId { get; set; }
[DataMember]
public string CityName { get; set; }
[DataMember]
public string StateId { get; set; }
[DataMember]
public string Priority { get; set; }
}
}
public class ServiceAPI : Service1
{
public List<City> GetCityCode()
{
adp = new SqlDataAdapter("Select * from tblCity", offcon);
adp.Fill(ds, "City");
var city = (from DataRow dr in ds.Tables["City"].Rows
select new
{
Id = dr["intCityId"].ToString(),
Name = dr["strTitle"].ToString(),
sid = dr["intStateId"].ToString(),
priority = dr["intPriority"].ToString()
}).Select(x => new City() { CityId = x.Id, CityName = x.Name, StateId = x.sid, Priority = x.priority }).ToList();
return city;
}
}
my web.config is as follows:
<?xml version="1.0"?>
<configuration>
<system.web>
<compilation debug="true" targetFramework="4.0" />
</system.web>
<system.serviceModel>
<services>
<service behaviorConfiguration="ServiceBehaviour" name="PatrikaAPIService.PatrikaService">
<endpoint address="" behaviorConfiguration="web" binding="webHttpBinding"
contract="PatrikaAPIService.IPatrikaService"/>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange"/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehaviour">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="web">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<serviceHostingEnvironment/>
</system.serviceModel>
</configuration>
Everything is working fine when i run this wcf on localhost as:
localhost:13186/ServiceApp.svc/GetCities
problem: when i pass my IP address as
192.168.1.16:13186/ServiceApp.svc/GetCities
it is giving an error that website is too busy...
& i want to access this wcf service in URL on other computers i mean other PCs on my Network.
I have changed my web.config as per my requirements now if anyone know what to do next to host this service with IP address.
or Host this into Microsoft 2003 Server SP2.
please help..

I'm assuming this is Cassini? If so - Cassini only responds if the host header is 'localhost' I think. Certainly you won't get it to respond by your IP.
Host it in IIS or IIS Express.
On a different note - if this is a .Net 4 project you might be interested to note that Rest services implemented this way via WCF is soon to become legacy - and be replaced by the Asp.Net Web API once it's gone to RTM (it's currently at RC stage) - I urge you to consider this newer technology if you can (it wouldn't solve this issue though).

Related

SwaggerWCF configuration for self hosted WCF library

I'm having some difficulties getting SwaggerWCF to load my documentation page, and I'm not sure why. I get no errors, but I also get no Swagger docs either, just a 404 when I visit http://localhost:8733/docs per the endpoint configuration. What am I doing wrong here? I have everything decorated up, using Framework 4.8. Service works fine and the mex and js endpoints will return data, just no swaggerUI.
Here is my App.Config:
<system.serviceModel>
<standardEndpoints>
<webHttpEndpoint>
<standardEndpoint name="" contentTypeMapper="Microsoft.Samples.WebContentTypeMapper.JsonContentTypeMapper, JsonContentTypeMapper, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"/>
</webHttpEndpoint>
</standardEndpoints>
<services>
<service name="AutodeskVaultAPI.VaultWorker">
<endpoint address="" binding="basicHttpBinding" contract="AutodeskVaultAPI.IVaultServices">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<endpoint address="js" behaviorConfiguration="jsonEP" binding="webHttpBinding"
name="jsonEP" contract="AutodeskVaultAPI.IVaultServices" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8733/AutodeskVaultAPI/" />
</baseAddresses>
</host>
</service>
<service name="SwaggerWcf.SwaggerWcfEndpoint">
<endpoint address="http://localhost:8733/docs" binding="webHttpBinding" contract="SwaggerWcf.ISwaggerWcfEndpoint" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior>
<serviceMetadata httpGetEnabled="True" httpsGetEnabled="True" />
<serviceDebug includeExceptionDetailInFaults="True" />
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="jsonEP">
<webHttp helpEnabled="true" automaticFormatSelectionEnabled="true"/>
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
Here is my service implementation:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[SwaggerWcf("/AutodeskVaultAPI/js")]
public class VaultWorker : IVaultServices
{
...[redacted]...
[SwaggerWcfTag("AutodeskVaultAPI")]
public AutodeskVaultFolder GetRootFolder(string vaultServerName = "", string currentUserLogin = "false")
{
try
{
Folder rootFolder = VaultConnection.WebServiceManager.DocumentService.GetFolderRoot();
if (null == rootFolder)
return null;
else
{
var toReturn = new AutodeskVaultFolder()
{
Created = rootFolder.CreateDate,
Category = (null == rootFolder.Cat) ? "No Category" : rootFolder.Cat.CatName,
CreatedByUserID = rootFolder.CreateUserId,
CreatedByUserName = rootFolder.CreateUserName,
EntityMasterID = rootFolder.Id,
FolderEntityName = rootFolder.Name,
FolderFullPath = rootFolder.FullName,
IsVaultRoot = true,
NumberOfChildren = rootFolder.NumClds,
ParentID = rootFolder.ParId
};
return toReturn;
}
}
catch (Exception ex)
{
Debug.WriteLine(ex);
return null;
}
}
[SwaggerWcfTag("AutodeskVaultAPI")]
public AutodeskVaultSearchResponse SearchVault(AutodeskVaultSearchRequest request)
{
try
{
string bookMark = string.Empty;
var parameters = getSearchParametersFromRequest(request);
SrchStatus srchStatus = null;
List<File> foundFiles = new List<File>();
if (null != parameters && parameters.Length > 0)
{
while (null == srchStatus || foundFiles.Count < srchStatus.TotalHits)
{
File[] srcResults = VaultConnection.WebServiceManager.DocumentService.FindFilesBySearchConditions(parameters, null, null, true, false, ref bookMark, out srchStatus);
if (null != srcResults)
foundFiles.AddRange(srcResults);
else
break;
}
}
return mapResultsToResponse(request, foundFiles);
}
catch (Exception ex)
{
Debug.Write(ex);
return null;
}
}
...[redacted]...
[DataContract(Name = "AutodeskVaultSearchRequest")]
public class AutodeskVaultSearchRequest
{
[DataMember]
public bool OR_Search = false;
[DataMember]
public List<AutodeskVaultProperty> properties;
}
[DataContract(Name = "AutodeskVaultSearchResponse")]
public class AutodeskVaultSearchResponse
{
[DataMember]
public AutodeskVaultSearchRequest Request;
[DataMember]
public List<AutodeskVaultFile> Files;
[DataMember]
public string Message;
and here is my service interface:
[ServiceContract]
public interface IVaultServices
{
[SwaggerWcfPath("GetRootFolder", #"Test the default configured server to see if we can get back the root folder")]
[OperationContract]
[WebInvoke(UriTemplate = "GetRootfolder/{vaultServerName}/{currentUserLogin}", Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[Description(#"Test the default configured server to see if we can get back the root folder")]
AutodeskVaultFolder GetRootFolder(string vaultServerName = "", string currentUserLogin = "false");
[SwaggerWcfPath("GetAsbuiltDrawingsByNumber", #"Given an Autodesk Search Request, search through Vault to find File information using the supplied properties.")]
[OperationContract]
[WebInvoke(UriTemplate = "SearchVault", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
[Description(#"Given an Autodesk Search Request, search through Vault to find File information using the supplied properties.")]
AutodeskVaultSearchResponse SearchVault(AutodeskVaultSearchRequest request);
}
Add an endpoint to your App.config file.
<services>
<service name="SwaggerWcf.SwaggerWcfEndpoint">
<endpoint address="http://localhost/docs" binding="webHttpBinding" contract="SwaggerWcf.ISwaggerWcfEndpoint" />
</service>
</services>
Create a WebServiceHost
var swaggerHost = new WebServiceHost(typeof(SwaggerWcfEndpoint));
swaggerHost.Open();
You can refer to the steps provided in the link for details.
https://github.com/abelsilva/swaggerwcf
How do I view my Swagger docs when using SwaggerWcf?

How to set up the required meta data for WCF restful service?

I am trying to get my first WCF restful service to work. But I keep getting a warning when i run it through VS2013. it says it cannot find any service metadata.
How do i set this up?
The App.config file is as follows,
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true" />
</appSettings>
<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="WcfJsonRestService.Service1">
<endpoint address="http://localhost:8732/service1"
binding="webHttpBinding"
contract="WcfJsonRestService.IService1"/>
</service>
</services>
<behaviors>
<endpointBehaviors>
<behavior>
<webHttp />
</behavior>
</endpointBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
The IService1.cs
namespace WcfJsonRestService
{
[ServiceContract]
public interface IService1
{
[OperationContract]
Service1.Person GetData(string id);
}
}
The Service1.cs
namespace WcfJsonRestService
{
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in both code and config file together.
public class Service1 : IService1
{
[WebInvoke(Method = "GET",
ResponseFormat = WebMessageFormat.Json,
UriTemplate = "data/{id}")]
public Person GetData(string id)
{
// lookup person with the requested id
return new Person()
{
Id = Convert.ToInt32(id),
Name = "Leo Messi"
};
}
public class Person
{
public int Id { get; set; }
public string Name { get; set; }
}
}
}

Exposing Metadata Exchange for use with svcutil

I am trying to take a very simple (essentially empty/function-less) service I have and generate a proxy with svcutil.exe. Here's my server:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.ServiceModel;
class Server2
{
static void Main(string[] args)
{
Console.WriteLine("Server");
Uri baseAddress = new Uri("http://localhost:1234");
var host = new ServiceHost(typeof(TheContractService), baseAddress);
//host.AddServiceEndpoint(typeof(TheContractService), new WSHttpBinding(), "ContractService");
host.Open();
Console.ReadLine();
}
}
[ServiceContract]
class TheContractService
{
[OperationContract]
void Expose()
{
Console.WriteLine("Exposed");
}
}
[DataContract]
class TheContract
{
[DataMember]
public string PublicProperty { get; set; }
[DataMember]
public string PublicField;
[DataMember]
private string PrivateProperty { get; set; }
[DataMember]
private string PrivateField;
[DataMember (Name = "BetterName")]
private string fudshjguhf;
}
Now I just need to set up my .config file to allow MEX -- here is my server config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<system.serviceModel>
<services>
<service name="ContractService"
behaviorConfiguration="MexServiceBehavior">
<endpoint address="ContractService"
binding="basicHttpBinding"
contract="TheContractService"
/>
<endpoint address="mex"
binding="mexHttpBinding"
contract="IMetadataExchange"
/>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="MexServiceBehavior">
<serviceMetadata httpGetEnabled="true"/>
</behavior>
</serviceBehaviors>
</behaviors>
</system.serviceModel>
</configuration>
What am I doing wrong here? When I try to run this command:
svcutil /t:code http://localhost:1234/mex /out ContractService.cs /config: ContractService.config
I get either 400 or 405 errors and the client proxy is not generated successfully. Can anyone see any issues with what I currently have? Thanks!
Your class name is TheContractService, but in your config the name attribute of the <service> element is ContractService. Make sure that the value of that attribute is the fully-qualified name (namespace, if any, plus the class name), otherwise the configuration won't be picked up.

WCF service selfhosting

I am creating a WCF service with self hosting. I have found the following error i.e.:
The target assembly contains no service types. You may need to adjust the Code Access Security policy of this assembly.
The codes are as follows:
namespace MyJobs
{
public interface IJobsSvc
{
[OperationContract]
DataSet GetJobs();
[OperationContract]
Job GetJobInfo(int JobId);
[OperationContract]
List<Job> GetAllJobs();
}
}
namespace MyJobs
{
[DataContract]
public class Job
{
[DataMember]
public int JobId { get; set;}
[DataMember]
public string Description{get;set;}
[DataMember]
public int MinLevel { get; set; }
[DataMember]
public int MaxLevel { get; set; }
}
}
namespace MyJobs
{
public class JobsSvc:IJobsSvc
{
#region IJobsSvc Members
public System.Data.DataSet GetJobs()
{
string str = #"data source=PERSONAL-659BE4;database=practice;integrated security=true";
DataSet ds = new DataSet();
SqlConnection cn = new SqlConnection(str);
SqlDataAdapter da = new SqlDataAdapter("select * from Job1",cn);
da.Fill(ds);
return ds;
}
public Job GetJobInfo(int JobId)
{
string str = #"data source=PERSONAL-659BE4;database=practice;integrated security=true";
SqlConnection cn = new SqlConnection(str);
SqlCommand cmd = new SqlCommand("select * from Job1 where JobId="+JobId,cn);
cn.Open();
SqlDataReader dr = cmd.ExecuteReader();
Job obj = new Job();
if (dr.Read())
{
obj.JobId = JobId;
obj.Description = dr[1].ToString();
obj.MinLevel = Convert.ToInt32(dr[2]);
obj.MaxLevel = Convert.ToInt32(dr[3]);
}
else
{
obj.JobId = -1;
}
return obj;
}
public List<Job> GetAllJobs()
{
throw new NotImplementedException();
}
#endregion
}
}
The app.config file is:
<?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="MyJobs.Job">
<endpoint address="" binding="wsHttpBinding" contract="MyJobs.IJobsSvc">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
<host>
<baseAddresses>
<add baseAddress="http://localhost:8732/Design_Time_Addresses/Jobs/MyJobs/" />
</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>
You need to add [ServiceContract] attribute to your IJobSvc interface
Update
Create the behavior to expose the metadata.
<serviceBehaviors>
<behavior name="SimpleServiceBehavior">
<!-- 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>
Then configure your service with this behavior:
<service name="MyJobs.Job" behaviorConfiguration="SimpleServiceBehavior">
<endpoint address="" binding="wsHttpBinding" contract="MyJobs.IJobsSvc">

Bad Request error on POST to WCF Restful web service Hosted in IIS using Basic Authentication over https

I have a wcf 3.5 restful web service that is hosted in IIS 7.5. It is setup to use Basic Authentication over https. I added as much information as possible below (names of classes, etc have been altered slightly). Any advice or suggestions are greatly appreciated.
Every time I try to send a POST either via Fiddler or through a client application I get this message:
HTTP/1.1 400 Bad Request
I turned on the svctrace logs give me this information:
System.Runtime.Serialization.SerializationException, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
Unable to deserialize XML body with root name 'user-search-request' and root namespace '' (for operation 'FetchMatchingUsers' and contract ('IMyService', 'http://tempuri.org/')) using DataContractSerializer. Ensure that the type corresponding to the XML is added to the known types collection of the service.
The URL for the service looks like this:
https://mysite.com/program/user/v1.0/MyService.svc
My Service Contract looks like this:
[ServiceContract]
interface IMyService
{
[WebInvoke(UriTemplate = "",
Method = "POST",
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml)]
[OperationContract]
Users FetchMatchingUsers(User u);
}
My Service Implementation looks like this:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
public class MyService : IMyService
{
static Users _users = new Users();
public Users FetchMatchingUsers(User u)
{
return SearchByFullName(u.FirstName, u.LastName);
}
}
The DataContract looks like this:
[CollectionDataContract(Name = "user-search-request", Namespace = "")]
public class Users : List<User>
{ }
[DataContract(Name = "user", Namespace = "")]
public class User
{
[DataMember(Name = "first-name")]
public string FirstName { get; set; }
[DataMember(Name = "last-name")]
public string LastName { get; set; }
}
Service Model is setup like this:
<system.serviceModel>
<diagnostics>
<messageLogging logMalformedMessages="true" logMessagesAtTransportLevel="true" />
</diagnostics>
<serviceHostingEnvironment aspNetCompatibilityEnabled="true">
</serviceHostingEnvironment>
<services>
<service behaviorConfiguration="basic" name="MyService">
<clear />
<endpoint address="/program/user/v1.0" binding="basicHttpBinding"
name="Default" contract="MyService.IMyService"
listenUriMode="Explicit">
<identity>
<certificateReference storeName="My" storeLocation="LocalMachine"
x509FindType="FindBySubjectDistinguishedName" />
</identity>
</endpoint>
<host>
<baseAddresses>
<add baseAddress="https://mysite.com" />
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="basic">
<serviceMetadata httpsGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<bindings>
<basicHttpBinding>
<binding name="basicHttpBinding">
<readerQuotas maxStringContentLength="10500" />
<security mode="Transport">
<transport clientCredentialType="Basic" />
</security>
</binding>
</basicHttpBinding>
</bindings>
My POST is setup like this:
private void DoPost
{
String firstname = "Test";
String lastname = "User";
User user = new User();
user.FirstName = firstname;
user.LastName = lastname;
String uristring = #"https://mysite.com/program/user/v1.0/MyService.svc/";
Uri uri = new Uri(uristring);
HttpWebRequest client = (WebRequest.Create(uri)) as HttpWebRequest;
NetworkCredential cred = new NetworkCredential(_username, _password, _domain);
String username = cred.Domain + "\\" + cred.UserName;
byte[] credentialBuffer = new UTF8Encoding().GetBytes(username + ":" + cred.Password);
client.Credentials = cred;
client.Method = "POST";
client.ContentType = "application/xml";
client.Headers["Authorization"] = "Basic " + Convert.ToBase64String(credentialBuffer);
List<User> users = new List<User>();
users.Add(user);
SerializeUsers sa = new SerializeUsers();
String output = sa.SerializeUsers(users);
client.ContentLength = output.Length;
// Write the request StreamWriter
StreamWriter requestWriter = new StreamWriter(client.GetRequestStream(), System.Text.Encoding.ASCII);
requestWriter.Write(output.ToString());
requestWriter.Close();
// Do the request to get the response
try
{
StreamReader responseReader = new StreamReader(client.GetResponse().GetResponseStream());
String response = responseReader.ReadToEnd();
responseReader.Close();
}
catch (WebException ex)
{
String message = ex.Message;
}
}
My Deserializer right now is just a test so the xml is actually hardcoded but I do need to make it work the right way once I get this problem solved.
public class SerializeUsers
{
public String SerializeUsers(List<User> ts)
{
String xml = #"<user-search-request xmlns:i=""http://www.w3.org/2001/XMLSchema-instance"">";
foreach (User t in ts)
{
xml += #"<user>";
xml += #"<first-name>" + t.FirstName + "</first-name>";
xml += #"<last-name>" + t.LastName + "</last-name></user>";
}
xml += #"</user-search-request>";
return xml;
}
}
Hello can you try [XmlSerializerFormat] under your service contract so it should look like this:
[ServiceContract]
[XmlSerializerFormat]
interface IMyService
The second thing you might want to do is add a wrapper to your POST like so:
[WebInvoke(UriTemplate = "",
Method = "POST",
BodyStyle = WebMessageBodyStyle.Wrapped,
RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml)]
Failing that then your config file is to blame if I was to hazard a guess, it should be set up with webHttpBinding.