简体   繁体   中英

IIS Error 500.2 -1 How do I deploy a simple custom Handler in IIS Windows 10?

I am trying to teach myself an IIS handler on Windows 10, IIS 10. Naturally, I started with a tutorial

MS Tutorial

It's old, but I can find nothing newer that is this comprehensive, and has the information gathered into one place.

The simple class and function is this:

using System;
using System.Web;
namespace IISHandler {
class MyHandler : IHttpHandler {
    public bool IsReusable
    {
        get { return true; }
    }
    public void ProcessRequest(HttpContext context) {
        DateTime dt;
        String useUTC = context.Request.QueryString["utc"];
        if (!String.IsNullOrEmpty(useUTC) &&
            useUTC.Equals("true")) {
            dt = DateTime.UtcNow;
        } else {
            dt = DateTime.Now;
        }
        context.Response.Write(
            String.Format("<h1>{0}</h1>", dt.ToLongTimeString()));

    }
 }
}

I created the assembly in VS 2015, and everything looks fine. Until I tried to "deploy" it. Then everything falls apart.

I've been digging for three days now. None of the information is in a single place. I will cover what I have done. First thing, is to copy the DLL into the /bin folder of the web application. That's fine.

Then Use the "Add Module" option in IIS Manager. Well, at the default level it gave me this: 添加到 GAC

Okay, I'm not adding this to the General assembly. All I want is a custom handler in my Web App.

Next, paying closer attention, I am adding it only to one Web App.

The tutorial says that the Add Module will discover it. Well, that didn't happen. I've got a list of things that appear to be "Built in", but not the DLL I created from the tutorial.

模块列表

So I manually just typed in the name "IISHandler.MyHandler", which is the namespace (assembly) and the class name. Then IIS Manager tells me this:

添加失败

No amount of web searching has given me any clue how to deal with it.

Next up, I decided to dig into the web.config file. Of course the MS page tells you that you can get all the detailed information... sometime in the future. 快来了

(This was published in 2008. But I've got such low expectations from MS doc, nothing surprises me)

I did manage to find a few other resources on the web.config. So while attempting to get a masters level degree in IIS XML Config files, I have zeroed in on the following file contents:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <handlers>
           <add name="MyTest" path="example.time" verb="*" resourceType="Unspecified"  
             modules="IISHandler.MyHandler" preCondition="integratedMode"  />
       </handlers>
     </system.webServer>
</configuration>

All this should do is intercept a page request "example.time", and return the current time according to the original C# code in the tutorial.

The response has been perpetually this:

500错误

It's not telling me it can't FIND the module, only that it's bad. But with Microsoft, there's no telling what the real problem is.

I tried (unsuccessfully) to just set module="IISHandler" even though that wouldn't make sense, as something still needs to know the class to invoke. I was right, it still didn't work.

And continuing to search, all over the web there were suggestions to execute this command (which didn't make sense as the "features" were already added through the control panel):

%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe -i

That gave me an error: cmd失败

(Telling me to install .NET 4.5 on my IIS 8 is just another MS deficiency)

My Add/Remove features for IIS 10 doesn't show 4.5 But it does show 4.8 添加删除

Is that significant? I have no idea, and can't find any documentation.

I have also insured that the AppPool is set to "Integrated", and this Web app is in that pool应用程序池

But my choice is only .NET 2.0 or .NET 4.0... And yet the Add/Remove offers me 3.5, and NOT 2.0? My assembly is build in .NET 4.8 (I retried in 4 as well... no luck). And there is no 4.0 as an option in the Add/Remove under IIS 10.

What is the kludge of 2.0, 3.5, 4.0, 4.5, 4.8??

But the real core on my question is: What is the process to get IIS to recognize my C# assembly and simply process a simple custom handler?

I hope this is enough information that someone can offer some assistance.

TIA

-SpacemanScott

Ironically enough, a fellow (remote) worker was trying to solve this same issue. He informed me he got it working a few hours after I posted this.

Here is the web.config:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
        <modules>
          <add name="MyModule" type="IISHandler.MyModule" />
        </modules>  
        <handlers>
            <add name="MyHandler" path="example.time" verb="*" resourceType="Unspecified" type="IISHandler.MyHandler" />
        </handlers>
    </system.webServer>
</configuration>

Although I still do not have the masters or PhD degree in IIS XML config files, the specific difference is that the <module> was added, and the "type" attribute was added to the <handler> to indicate the specific handler function.

Also the "module" attribute was removed from the <handler>, although this page seemed to indicate that was how the server knew where to find the module that provides the handler. Apparently not.

The <module> is used to handle all requests during the processing sequence, and since that ability was not desired, it was not hand entered. But apparently it is still necessary to tell the configuration that you will be using that assembly module.

If anyone runs into a similar problem, perhaps this will help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM