How can I use a dll from GAC inside Web User Control
Actually my objective is;
- Create asp.net web service (namespace ws, class Service1)
- Create dll from web service
- Install this dll to GAC
- Create ASP.NET web application
- Don't add a reference for this dll in the GAC
- Create Web User Control without any code behind the file
- Write code inside the Web User Control to use the Web Service created above (without giving reference to application)
The question is;
- Can the web application searchs assembly inside GAC even if the assembly wasn't referenced to the application
- Can I use this web user control(in this situation UC won't be compiled because it isn't code behinded) in other pr开发者_Python百科ojects like WebParts?
PS: My last situation: Service1.asmx.cs :
using System.Web.Services;
namespace ws
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
WebUserControl1.ascx :
<%@ Control Language="C#" ClassName="wuc" %>
<script runat="server">
protected override void CreateChildControls()
{
base.CreateChildControls();
ws.Service1 srv = new ws.Service1();
btn.Text = srv.HelloWorld();
}
</script>
<asp:Button runat="server" ID="btn" Text="asdasd" />
Default.aspx :
...
<%@ Register src="WebUserControl1.ascx" tagname="WebUserControl1" tagprefix="uc1" %>
...
<form id="form1" runat="server">
<div>
<uc1:WebUserControl1 ID="WebUserControl11" runat="server" />
</div>
</form>
...
I compiled and web page came up with this error:
My objective is;
- Create asp.net web service (namespace ws, class Service1)
- Create dll from web service
- Install this dll to GAC
Aaaaaaahhhhhh!
Why do you need to install assemblies to the GAC at runtime?! No good can come of this!
For what its worth the answer to both your questions is yes - you can either load the assembly yourself, either directly using something like Assembly.Load or indirectly by (for example) creating a page which attempts to load a type using an assembly qualified type name.
I have learnt and posted to my blog.
First we need to generate dll from webservice:
@SET WSDL="c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\wsdl.exe"
%WSDL% /language:cs /out:c:\temp\servis.cs http://localhost:85/WebService1.asmx?WSDL
We will deploy our webservice's dll to GAC. So we need to have snk file. We will create dll file from our cs but before we need to create snk.
@SET SN="c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\sn.exe"
%SN% -k c:\temp\servis.snk
Now we can generate dll from cs file
@SET CSC="c:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe"
%CSC% /target:library /out:c:\temp\servis.dll c:\temp\servis.cs /keyfile:C:\temp\servis.snk
Now we can deploy this dll to GAC
@SET GACUTIL="c:\Program Files (x86)\Microsoft SDKs\Windows\v7.0A\Bin\NETFX 4.0 Tools\gacutil.exe"
%GACUTIL% -if c:\temp\servis.dll
We can learn FullName of our dll in the GAC
%GACUTIL% /l servis.dll
in .ascx file we can write these lines inside to script runat=server
tag
string sFullName = "servis, version=1.0.0.0 publickeytoken=asdhfasdfjk2323"
string sClass = "Service1"
object instance = Activator.CreateInstance(sFullName, sClass).Unwrap();
Type tip = instance.GetType();
MethodInfo mi = tip.GetMethod("HelloWorld");
object result = mi.Invoke(instance,null);
string sResult = result.ToString();
精彩评论