I'm having trouble launching my WCF service using a console application. What can be wrong? - wcf

Here's my code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using BankServiceClient.BankServiceReference;
namespace BankServiceClient
{
class Program
{
static void Main(string[] args)
{
Uri baseAddress = new Uri("http://localhost:8000/Simple");
Type instanceType = typeof(BankServiceReference.BankClient);
ServiceHost host = new ServiceHost(instanceType,baseAddress);
using (host)
{
Type contractType = typeof(BankServiceReference.IBank);
string relativeAddress = "BankService";
host.AddServiceEndpoint(contractType, new BasicHttpBinding(), relativeAddress);
host.Open();
Console.WriteLine("Press <ENTER> to quit.");
Console.ReadLine();
host.Close();
}
/*
* Consuming a WCF Service and using its method.
*/
//IBank proxy = new BankClient();
//double number = proxy.GetBalance(1234);
//Console.WriteLine(number.ToString());
//Console.ReadLine();
}
}
}
First, a couple of questions:
The 'baseAddress' attribute, what exactly is it? When I launched my service using the default F5 (no console application) the service launched on a random port on localHost. How can I write in an exact number and expect it to go there? Confused at this one.
What is the relativeAddress attribute? It says BankService but what should I write in that attribute? Confused at this one as well.
Here's the exact error message I get when I try to run this Console application:
HTTP could not register URL
http://+:8000/Simple/. Your process
does not have access rights to this
namespace (see
http://go.microsoft.com/fwlink/?LinkId=70353
for details).

First is your client project set to be the start up project?
And to answer your questions.
1) baseAddress (URI Class) is the base address for your hosted service. I am thinking you are launching some other project.
2) You have two options on configuring endpoints(reference). Relative and Absolute. The way you did it will take your base and appends your relative -> http://localhost:8000/Simple/BankService
Lastly to fix your hosting issue see this SO link:
WCF ServiceHost access rights

Related

Third party IIS Module not printing to log files

I have an asp.net core application hosted and webdav enabled in IIS for storing files as well. I want to know when a user saves a file in the webdav server (ie, if opened and saved in Microsoft word). I have put together this IIS module to try and intercept all http requests so I can find out what a webdav HTTP requests look like and then hopefully create a if statement using this logic once I can identify the save request. So far here is the IIS module I have created:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
namespace WebDAVTargetDetectorIIS
{
public class LoggingModule : IHttpModule
{
#region IHttpModule Members
public void Dispose()
{
}
public void Init(HttpApplication context)
{
context.PreRequestHandlerExecute += new EventHandler(OnPreRequestHandlerExecute);
context.PostLogRequest += new EventHandler(PostLogEvent);
}
#endregion
public void OnPreRequestHandlerExecute(Object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpRequest request = app.Context.Request;
if (!String.IsNullOrEmpty(request["HTTP_CF_CONNECTING_IP"]))
{
request.ServerVariables.Set("REMOTE_ADDR", request["HTTP_CF_CONNECTING_IP"]);
}
}
public void PostLogEvent(Object source, EventArgs e)
{
HttpApplication app = (HttpApplication)source;
HttpRequest request = app.Context.Request;
app.Response.AppendToLog("This is a test");
}
}
}
I have created the BIN folder in the root folder of my web application and used the IIS Manager to add the new module. It is added to the end of the ordered list. The log files in IIS seem to save to "C:\inetpub\logs\LogFiles\W3SVC1" however my module does not seem to be printing my test line in there. Is there a particular reason the code above would not implement on any even a login request? Or should I be attaching this to some other event?

Managed CSOM on Sharepoint 201. Could not create SSL/TLS secure channel

I have a Windows Server 2016 VM and Visual Studio 2017 Community Edition. I am writing a simple script just to check the connectivity with the Sharepoint 2013. The Sharepoint Server is NOT installed on the VM, it is somewhere within the intranet.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using System.Security;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
string siteUrl = "https://xxx.yyy.org:443";
ClientContext clientContext = new ClientContext(siteUrl);
Web site = clientContext.Web;
List documents = site.Lists.GetByTitle("Documents");
//Prepare the request
clientContext.Load(site);
//Submit the Request
clientContext.ExecuteQuery();
Console.ReadKey();
}
}
}
But I am getting this exception:
System.Net.WebException occurred
HResult=0x80131509
Message=The request was aborted: Could not create SSL/TLS secure channel.
Source=System
StackTrace:
at System.Net.HttpWebRequest.GetResponse()
at Microsoft.SharePoint.Client.SPWebRequestExecutor.Execute()
at Microsoft.SharePoint.Client.ClientContext.GetFormDigestInfoPrivate()
at Microsoft.SharePoint.Client.ClientContext.EnsureFormDigest()
at Microsoft.SharePoint.Client.ClientContext.ExecuteQuery()
at ConsoleApp1.Program.Main(String[] args) in C:\Users\AdminDD\source\repos\ConsoleApp1\ConsoleApp1\Program.cs:line
I've read this thread and I signed in through IE. The certificate of the browser seems valid and the problem remains. Is there any suggestion on how to solve this ?
I had the exact same issue.
Check your project .Net Version. It worked after upgrading from 4.5 --> 4.6
you should do a request on your url https://yoursite/ absolute url only, nothing more
$response = Invoke-WebRequest -Verbose -URI $anUrl -CertificateThumbprint $CertificateThumbprint -UseDefaultCredentials -SessionVariable websession -ErrorAction:Stop
get cookie authentification in reponse, and then add it in your csom query
$request.ClientCertificates.Add($global:cert)
$request.CookieContainer = New-Object System.Net.CookieContainer
$c3 = New-Object System.Net.Cookie($global:cookieName3, $global:cookieVal3, "/", $global:cookieDomaine);
$request.CookieContainer.Add($c3);
here is a working sample

Ninject is not generating WSDL

I just started playing with Ninject for self hosted WCF services.
I ran into a problem where it isnt generating a wsdl (url?wsdl or url?singleWsdl).
I start up the service with this :
private static void StartNinjectSelfHost()
{
var someWcfService = NinjectWcfConfiguration.Create<CalculatorService, NinjectWebServiceSelfHostFactory>();
_selfHost = new NinjectSelfHostBootstrapper(CreateKernel,someWcfService);
_selfHost.Start();
}
If I revert to the standard way with this:
private static void LoadWcf()
{
if (serviceHost != null)
{
serviceHost.Close();
}
// Create a ServiceHost for the CalculatorService type and
// provide the base address.
serviceHost = new ServiceHost(typeof(CalculatorService));
// Open the ServiceHostBase to create listeners and start
// listening for messages.
serviceHost.Open();
}
Then I get the wsdl just fine at this URL:
http://localhost:8000/ServiceModelSamples/service?singleWsdl
I'm guessing I have to tell Ninject to do this, but I'm struggling to find any good info by searching.
Any help on enabling the wsdl is appreciated.
Nevermind I'm dumb. I wanted to use "NinjectServiceSelfHostFactory" instead, now it works

Hosting WCF service on linux

Is there Any way of hosting WCF service on Linux.
I read about wine but i didn't see any example of hosting WCF service with it.
P.S : I have tried mono and mod_mono but to no avail.
You can host it in a stand-alone console application like so:
using System;
using System.ServiceModel;
using Service;
namespace Host
{
class MainClass
{
public static void Main (string[] args)
{
Console.WriteLine ("WCF Host!");
var binding = new BasicHttpBinding ();
var address = new Uri ("http://localhost:8080");
var host = new ServiceHost (typeof(GreeterWcfService));
host.AddServiceEndpoint (
typeof(IGreeterWcfService), binding, address);
host.Open ();
Console.WriteLine ("Type [Enter] to stop...");
Console.ReadLine ();
host.Close ();
}
}
}
Where GreeterWcfService is the WCF service class itself and IGreeterWcfService is the service contract.
Full working example solution in GitHub - with separate projects for the service, the hosting and a client. Check it out.
Its possible but you should refer to this link for understanding current state and known issues - http://www.mono-project.com/docs/web/wcf/. It's limited now. For eg. if you wish to use WSHttpBinding its not supported currently.

Pass an object as parameter to the ServiceHost

I'm trying to start up a project which takes care of stuff. I call this class the CentralLogic class. When this is started, I'd like to start a host. The host has some settings in the App.Config file like the baseAddress and endpoints.
This host can interact with this CentralLogic class. I'd like to pass on the instantiated object to this host.
Currently, I'm trying something like this:
centralLogic = new CentralLogic();
ServiceHost host = new ServiceHost(centralLogic, typeof(KLAService));
using (host)
{
host.Open();
//Start a WPF UI. Also makes sure the host stays open
//as long as the UI stays open.
Application app = new Application();
app.Run(new ConfigurationWPF.MainWindow(centralLogic));
host.Close();
}
The KLAService is defined like this:
public class KLAService : IKLAService
{
CentralLogic centralLogic;
public KLAService(CentralLogic theCentralLogic)
{
centralLogic= theCentralLogic;
}
.....
}
This doesn't work (the creation of the ServiceHost is wrong, both in number of arguments and the second parameter). I can get it started without parameter by doing:
ServiceHost host = new ServiceHost(typeof(KLAService));
So, the problem is that I don't know how to pass on the object on to the server. How do I do this?
EDIT: I tried the following:
centralLogic = new CentralLogic();
KLAService klaService = new KLAService(centralLogic);
using (ServiceHost host = new ServiceHost(klaService))
{
host.Open();
Application app = new Application();
app.Run(new ConfigurationWPF.MainWindow(centralLogic));
host.Close();
}
This popped an InvalidOperationException:
In order to use one of the ServiceHost constructors that takes a
service instance, the InstanceContextMode of the service must be set
to InstanceContextMode.Single. This can be configured via the
ServiceBehaviorAttribute. Otherwise, please consider using the
ServiceHost constructors that take a Type argument.