C# HTTPModule Could not load type CGI Request - cgi

Trying to use a HTTPModule I wrote in complied C# class project to log request values and extend a third party CGI shopping cart. My module works fine with asp,asp.net,jpg and html request, but soon as I request the store.cgi, I get the following error. Do I have to do something special in IIS7 or does HTTPModule not work with a CGI executables running in CGI-BIN?
Server Error in '/cgi-bin' Application.
Could not load type 'IISWatcher.WatchRequests'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Web.HttpException: Could not load type 'IISWatcher.WatchRequests'.
Source Code:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Messaging;
namespace IISWatcher
{
public class WatchRequests : IHttpModule
{
public void Init(System.Web.HttpApplication app)
{
app.BeginRequest += new EventHandler(app_BeginRequest);
app.EndRequest += new EventHandler(app_EndRequest);
}
void app_EndRequest(object sender, EventArgs e)
{
//HttpApplication app = (HttpApplication)sender;
}
void app_BeginRequest(object sender, EventArgs e)
{
string strReturn = "\r\n";
HttpApplication app = (HttpApplication)sender;
string strAddress = app.Request.UserHostAddress;
string strUrl = app.Request.Url.AbsoluteUri;
string strQS = app.Request.QueryString.ToString();
RequestInfo ri = new RequestInfo();
System.Diagnostics.EventLog.WriteEntry("HttpModule",
"IpAddress: " + strAddress + strReturn + "URL:" + strUrl);
System.Messaging.MessageQueue msq = new MessageQueue(#".\private$\HttpModuleQueue");
ri.AbsoluteUri = strUrl;
ri.IPAddress = strAddress;
ri.QueryString = strQS;
msq.Send(ri);
}
public void Dispose()
{
}
}
public class RequestInfo
{
public string IPAddress;
public string AbsoluteUri;
public string QueryString;
}
}
Web.config for IIS7:
...................

Related

Why does 'InputField' not contain a definition for 'text'?

I'm currently working on a Unity project for a college assignment, and I'm currently trying to connect a login/registration through PlayFab into a teammate's main menu for the game.
I've connected the PlayFabManager.cs script to the Input Fields for the email and password in the Unity editor, and something about my InputFields.cs file is preventing me from making any more progress.
I had to change the passwordInput and emailInput variables to TMP_InputField variables to achieve this, but now I am getting a compilation error in my project that says the following:
Assets\Scripts\InputField.cs(13,24): error CS1061: 'InputField' does not contain a definition for 'text' and no accessible extension method 'text' accepting a first argument of type 'InputField' could be found (are you missing a using directive or an assembly reference?)
Most places I look have people not including the "using UnityEngine.UI;" header at the top of the file, but that's included in my InputField.cs file.
Here's the code for my InputField.cs file:
using UnityEngine;
using System.Collections;
using UnityEngine.UI; // Required when Using UI elements.
public class InputField : MonoBehaviour
{
public InputField mainInputField;
public void Start()
{
mainInputField.text = "Enter text here...";
}
}
Here's the code for my PlayFabManager.cs file:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PlayFab;
using PlayFab.ClientModels;
using Newtonsoft.Json;
using UnityEngine.UI;
using TMPro; // Needed for login input fields
public class PlayFabManager : MonoBehaviour
{
[Header("UI)")]
public Text messageText;
public TMP_InputField emailInput;
public TMP_InputField passwordInput;
// Register/Login/ResetPassword
public void RegisterButton() {
var request = new RegisterPlayFabUserRequest {
Email = emailInput.text,
Password = passwordInput.text,
RequireBothUsernameAndEmail = false
};
PlayFabClientAPI.RegisterPlayFabUser(request, OnRegisterSuccess, OnError);
}
void OnRegisterSuccess(RegisterPlayFabUserResult result) {
messageText.text = "Registered and Logged in";
}
public void LoginButton() {
}
// Start is called before the first frame update
void Start() {
Login();
}
// Update is called once per frame
void Login() {
var request = new LoginWithCustomIDRequest {
CustomId = SystemInfo.deviceUniqueIdentifier,
CreateAccount = true
};
PlayFabClientAPI.LoginWithCustomID(request, OnSuccess, OnError);
}
void OnSuccess(LoginResult result) {
Debug.Log("Successful login/account create.");
}
void OnError(PlayFabError error) {
Debug.Log("Error while loggin in/creating account.");
Debug.Log(error.GenerateErrorReport());
}
}
I would just remove the InputField.cs class as it fixes my errors, but it changes the functionality of the following code that my teammate has contributed:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class MenuControl : MonoBehaviour
{
public string newGameLevel;
public void NewUser() {
SceneManager.LoadScene(newGameLevel);
}
public void ExitButton() {
Application.Quit();
}
}
Any help would be much appreciated!
Wanted to provide the solution in case this happens to anyone in the future:
I solved the problem by changing the
public InputField mainInputField;
into an input variable that could receive the TMP_Imput like so: public TMPro.TMP_InputField mainInputField;

Hanfire not logging custom exception with metadata

I have asp.net core 2.1 application along with HangFire 1.6.17. HangFire is configured to execute a background job at certain interval. The background job calls external API using HttpClient. If the http call fails, then the method throws custom exception with metadata. Idea is hangfire will log the exception with metadata. I followed best-practices-for-exceptions to create exception
public class MyHttpRequestException : Exception
{
public string Content { get; private set; }
public string RequestUri { get; private set; }
public string HttpResponse { get; private set; }
public MyHttpRequestException()
{
}
public MyHttpRequestException(string message)
: base(message)
{
}
public MyHttpRequestException(string message, Exception innerException)
: base(message, innerException)
{
}
public MyHttpRequestException(string message, string content, string httpResponse, string requestUri)
: base(message)
{
Content = content;
RequestUri = requestUri;
HttpResponse = httpResponse;
}
public override string ToString()
{
StringBuilder sb = new StringBuilder();
sb.Append(base.ToString());
sb.AppendLine();
sb.AppendLine();
sb.AppendLine("Content");
sb.AppendLine(Content);
sb.AppendLine("RequestUri");
sb.AppendLine(RequestUri);
sb.AppendLine("HttpResponse");
sb.AppendLine(this.HttpResponse);
return sb.ToString();
}
}
I also have extension method for HttpResponseMessage which ensures API request is successful, and if not throws MyHttpRequestException
public static class HttpResponseMessageExtensions
{
public static async Task EnsureSuccessStatusCodeAsync(this HttpResponseMessage response)
{
if (response.IsSuccessStatusCode)
{
return;
}
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
var httpResponse = response.ToString();
var requestUri = response.RequestMessage.RequestUri.ToString()
if (response.Content != null)
response.Content.Dispose();
throw new MyHttpRequestException("Error while making http request.", content, httpResponse, requestUri);
}
}
Here is my background Job which is invoked by Hangfire recurring job scheduler
public async Task DoSomething(string url)
{
var response = await _httpClient.GetAsync(url)
await response.EnsureSuccessStatusCodeAsync();
// do something here if everything is okay
}
Issue
When EnsureSuccessStatusCodeAsync method throws MyHttpRequestException then Hangfire logs the exception as expected, and i see that in HangFire's dashboard. However Hangfire only logs Exception message and stack trace. I don't see my custom properties are being logged ( ie. Content, RequestUri, HttpResponse)
In clssic .NET we use SerializationInfo like this SO post
How do i create a custom exception in .NET Core so metadata will also get logged?
Note:
When the MyHttpRequestException gets thrown i noticed exception's ToString() method is getting called
however, i dont see whatever ToString() returns is getting logged by Hangfire.
I dont know if this is hangfire issue, or i need to implement MyHttpRequestException is different way.
The stack trace that you see in Dashboard is formatted. You can see here and here.
Because this custom stack trace format you can see your custom properties.

Self-host (No IIS or WAS) WCF with a service that requires parameters

Hopefully this is an easy one. I'm wondering if this is possible - perhaps it is not. I'm attempting to self-host a WCF service (in my example below it is a console application). The service does not have a default constructor. It only contains a single parameter signature constructor. I need the service to be able to handle user sessions. Currently I am using Ninject DI. Here is a simple code solution I came up with to demonstrate my issue:
using System;
using System.ServiceModel;
using System.ServiceModel.Web;
using Ninject.Modules;
namespace ConsoleApplication1
{
public class Program
{
static void Main()
{
using (var webServiceHost = new WebServiceHost(typeof(MyWcf)))
{
var webHttpBinding = new WebHttpBinding();
var uri = new Uri("http://localhost:8000/");
webServiceHost.AddServiceEndpoint(typeof(IMyWcf), webHttpBinding, uri);
webServiceHost.Open();
Console.WriteLine("Service is ready...");
Console.ReadKey();
}
}
}
[ServiceContract]
public interface IMyWcf
{
[OperationContract, WebGet(UriTemplate = "")]
string HelloWorld();
}
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class MyWcf : IMyWcf
{
private readonly IMessage _customMessage = new Message("Default Message.");
public MyWcf(IMessage message)
{
_customMessage = message;
}
public string HelloWorld()
{
return _customMessage.Text;
}
}
public interface IMessage
{
string Text { get; }
}
public class Message : IMessage
{
public Message (string message)
{
Text = message;
}
public string Text { get; set; }
}
public class NinjectSetup : NinjectModule
{
public override void Load()
{
Bind<IMessage>().To<Message>()
.WithConstructorArgument("message", "Injected String Message.");
}
}
}
Obviously commenting out the parameterized constructor allows the service to run. But that does me no good. I don't want to use ServiceHostFactory because that apparently requires me to have a .svc/IIS. Is there a way around this? Can I just create a new MyWebServiceHost that inherits from WebServiceHost and override some method that will create a instance for the service?
Using Ruben's suggestion (in the comments) above, I was able to locate a working example within the Ninject.Extensions.Wcf source repository.

Error Object reference not set to an instance of an object on WCF Service

I am currently developing a WCF Publish Subscribe service. My Service has the following code,
public void PublishPost(string postSampleData)
{
PostChangeEventArgs e = new PostChangeEventArgs();
e.PostData = postSampleData;
PostChangeEvent(this, e);
}
and the code for the postChangeEvent is
public class PostChangeEventArgs : EventArgs
{
public string PostData;
}
and in my client file, i wrote this code in the main method,
class Program : IPostingContractCallback
{
static void Main()
{
InstanceContext site = new InstanceContext(null, new Program());
PostingContractClient client = new PostingContractClient(site);
WSDualHttpBinding binding = (WSDualHttpBinding)client.Endpoint.Binding;
String clientcallbackaddress = binding.ClientBaseAddress.AbsoluteUri;
clientcallbackaddress += Guid.NewGuid().ToString();
binding.ClientBaseAddress = new Uri(clientcallbackaddress);
client.Subscribe();
}
public void PostReceived(string postSampleData)
{
MessageBox.Show("PostChange(item {0})", postSampleData);
}
}
and for the code for my data source...
class Program : IPostingContractCallback
{
static void Main(string[] args)
{
InstanceContext site = new InstanceContext(new Program());
PostingContractClient client = new PostingContractClient(site);
client.PublishPost("testing");
Console.WriteLine();
Console.WriteLine("Press ENTER to shut down data source");
Console.ReadLine();
//Closing the client gracefully closes the connection and cleans up resources
client.Close();
}
public void PostReceived(string postSampleData)
{
Console.WriteLine("PostChange(item {0})",postSampleData);
}
}
After running the service, followed by the client, followed by the datasource, I'm suppose to receive a popup messagebox from my client. However there gives an error on the line
PostChangeEvent(this, e);
Object reference not set to an instance of an object.
Anyone know how to solve this?
It sounds like there's nothing subscribed to the event. To check for this, you should use:
var handler = PostChangeEvent;
if (handler != null)
{
handler(this, e);
}
That will stop the NullReferenceException, but of course it won't address why there were no subscribers... you haven't shown anything which subscribes to the event - what were you expecting to be subscribed?

Using a Callback to pass an Event to a WCF Client

I am trying to have my WCF client receive info from a callback. I have created a Client Library that any WCF Client can use to connect to my WCF Service. I am uncertain if I should implement the Callback in the Client Library or the WCF Client itself.
I have attempted to create an event that will be fired by calling the OnNotification(...) method from within the callback. However, it cannot be called from within the Callback method and I'm not sure why.
Here is my Client Library used to connect to the WCF Service:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel; //needed for WCF communication
namespace DCC_Client
{
public class DCCClient
{
private DuplexChannelFactory<ServiceReference1.IDCCService> dualFactory;
public ServiceReference1.IDCCService Proxy;
public DCCClient()
{
//Setup the duplex channel to the service...
NetNamedPipeBinding binding = new NetNamedPipeBinding();
dualFactory = new DuplexChannelFactory<ServiceReference1.IDCCService>(new Callbacks(), binding, new EndpointAddress("net.pipe://localhost/DCCService"));
}
public void Open()
{
Proxy = dualFactory.CreateChannel();
}
public void Close()
{
dualFactory.Close();
}
/// <summary>
/// Event fired an event is recieved from the DCC Service
/// </summary>
/// <param name="e"></param>
protected virtual void OnNotification(EventArgs e)
{
if (Notification != null)
{
Notification(this, e);
}
}
}
public class Callbacks : ServiceReference1.IDCCServiceCallback
{
void ServiceReference1.IDCCServiceCallback.OnCallback(string id, string message, Guid key)
{
//Can't call OnNotification(...) here?
}
}
}
OnNotification(...) cannot be called in the Callback method.
Here is an example of my how my WCF Client would be implemented using an EventHandler:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DCC_Client;
namespace Client_Console_Test
{
class Program
{
private static DCCClient DCCClient;
static void Main(string[] args)
{
try
{
DCCClient = new DCCClient();
DCCClient.Notification += new EventHandler(DCCClient_Notification);
DCCClient.Open();
DCCClient.Proxy.DCCInitialize();
Console.ReadLine();
DCCClient.Proxy.DCCUninitialize();
DCCClient.Close();
}
catch (Exception e)
{
DCCClient.Log.Error(e.Message);
}
}
static void DCCClient_Notification(object sender, EventArgs e)
{
//Do something with this event
}
}
}
Is this the correct way to pass the callback info to my WCF Client? I feel like adding an EventHandler is redundant and I should just use the callback itself. Am I correct to have implemented the Callback in my Client Library, or should this be done in each WCF Client?
Thank you in advance.
I think I figured it out. I simply need to pass the DCCClient reference to the callback, and then call OnNotification() from it.
In DCC_Client:
public class DCCClient
{
private DuplexChannelFactory<ServiceReference1.IDCCService> dualFactory;
private Callbacks notificationCallback; //Add callback object here
public ServiceReference1.IDCCService Proxy;
public DCCClient()
{
//Setup the duplex channel to the service...
NetNamedPipeBinding binding = new NetNamedPipeBinding();
notificationCallback = new Callbacks(this); //Pass DCCClient reference here
dualFactory = new DuplexChannelFactory<ServiceReference1.IDCCService>(notificationCallback, binding, new EndpointAddress("net.pipe://localhost/DCCService"));
}
//....
public class Callbacks : ServiceReference1.IDCCServiceCallback
{
private DCCClient client;
public Callbacks(DCCClient client)
{
this.client = client; //grab client refernce
}
void ServiceReference1.IDCCServiceCallback.OnCallback(string id, string message, Guid key)
{
client.OnNotification(n); //send the event here
}
}