English 中文(简体)
ASP.net C# - WINWORD.exe
原标题:

I am playing with Microsoft word document from ASP.NET/C# program.

My programs opens up word docs internally. using

app = new Word.Application();
app.Documents.Open

it creates a instance of winword.exe process, I can see it in Task Manager.

even if I closes Doc using close() and app.quit(). it should kill the process. but this process doesnt get killed.

any idea how to kill this process programmatically.

问题回答

To begin, I agree with Arthur and Ivo: don t use Word automation on the server... it s not supported and it s painful.

  1. If you can, try supporting Word input/output via the DOCX format (fully documented XML format packaged using compression that s supported by the .NET Framework and supported by pre-Office 2007 with a plugin)
  2. If not create a service that handles the requests in a queue to prevent multiple threads using it at the same time (ala Ivo s answer)

If you must use Word automation:

  1. Make sure you are setting as many Application options as possible to prevent dialogs
  2. Make sure you are passing in WdSaveOptions.wdDoNotSaveChanges to Document.Close
  3. Have a look through this document: How To: Dismiss a Dialog Box Displayed by an Office Application with Visual Basic .NET (also applies to C#, obviously)
  4. Make sure you call Marshal.ReleaseComObject on both the Document and Application objects, and make sure it s in a finally block.

The winword.exe should go away if you follow the above, you should not need to kill the process.

Edit: Not really an answer to your question, but on topic nonetheless. Keep in mind that you re also going to have problems on the server with permissions. Make sure you create a specific user for it and use that user as your AppPool identity (or service identity). You ll then need to do the following:

  1. Make sure you ve installed the automation components, obviously
  2. Run "dcomcnfg"
  3. Component Services -> Computers -> My Computers -> COM+ Applications
  4. "Microsoft Word document" (or "{000209FF-0000-0000-C000-000000000046}") -> Properties
  5. Security tab -> Launch and Activation permissions -> Customize
  6. Click "Edit" and add your AppPool user from above
  7. Repeat steps 5-6 but select "Access Permissions"

On top of all that, you still might need to run winword.exe once as your AppPool user directly from the server, despite the dialog options above, to initially setup the profile.

Office apps do not "destroy" the first instance created. You will find the same issue when using excel.

See this and others

50 Ways to Kill Excel

How to kill a process given a name

Look into System.Diagnostics.Process.GetProcessesByName and System.Diagnostics.Process.Kill

Try something like

string processName = "ProcessName";
Process[] processes = Process.GetProcessesByName(processName);

foreach(Process process in processes)
{
    process.Kill();
}

You should be able to grab each process of word and kill it:

foreach(Process p in System.Diagnostics.Process.GetProcessesByName("winword"))
 {
   p.kill();
   p.WaitForExit();
 }

Also you should try seeing the document object to null?

Most safe approach for using (Microsoft) Office on the server is to create a service that can accept streams and creates an instance of Word (or any other Office app) do it s thing and then kills the app. If you do not do this a reference to the running office app will remain within your asp worker process. This means that a lot of processes will keep running until your asp worker process is recycled. This is a large potential memory leak.

This is how we did it. Costs a lot of time to build this correctly but is the most safe way.

This was driving me crazy until I found this post on this site. The second solution on that page worked for me - the first one looks simpler, but it actually can get difficult to keep track of all the hidden COM objects you have a reference to. The second method always works - you don t have to track every object.

Just to be said: Office Applications are not supposed to run on a server. They are also not to supposed to be uses from a Service or WebApplication.

Yes - we also had requirements that forced us to use Office Products at the Server. And we always hat troubles.

If there is a chance not to use Office (dealing with WordML, the new Office XML format) then do that.





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签