I am creating a web application with MVC3 and MEF. I am attempting to export my HostModel to the plugin with an IDNumber and then have the plugin redirect to a link using that IDNumber. My model is not being exported correctly and then my view is also not reading the model from the plugin controller (I tested this by making a test model object and passing that to the view). I’m pretty sure I have some mix up with how I am exporting to the view in my plugin. I am using a .aspx file and not a .vbhtml because VS2010 didn’t give me the option. When I tried to drag a .vbhtml file into the project it didn’t work correctly.
Here is my HostController:
<Export(GetType(HostModel))>
<PartCreationPolicy(CreationPolicy.NonShared)>
Public Class HostController
Inherits System.Web.Mvc.Controller
Private m_objHost As HostModel
Private m_IDNUmber As String
Property IDNumber() As String
Get
Return m_IDNUmber
End Get
Set(value As String)
m_IDNUmber = value
End Set
End Property
Function Index() As ActionResult
ViewData("Message") = "Welcome to ASP.NET MVC!"
If m_objHost Is Nothing Then
m_objHost = New HostModel
End If
Return View(m_objHost)
End Function
'<HttpPost()>
Function ChangeCUNumber(model As HostModel, strIDNumber As String) As ActionResult
' m_IDNUmber = strIDNumber
model.IDNumber = strIDNumber
Return View("Index", model)
End Function
<HttpPost()>
Function GoToMini(model As HostModel) As ActionResult
m_CUNUmber = model.CUNumber
Dim hostContollerObj As New HomeController
hostContollerObj.CUNumber = model.IDNumber
m_objHost = model
Return Redirect("http://localhost:3727/miniView")
End Function
End Class
This is my HostModel:
Public Class HostModel
Implements IHost
Private Shared m_instance As HostModel
Private m_IDNumber As String
Public Sub New()
End Sub
Shared ReadOnly Property instance() As HostModel
Get
If m_instance Is Nothing Then
m_instance = New HostModel
End If
Return m_instance
End Get
End Property
Public Property IDNumber As String Implements CUCMCV_Interfaces.IHost.IDNumber
Get
Return m_IDNumber
End Get
Set(value As String)
m_IDNumber = value
End Set
End Property
This is my plugin controller:
<Export(GetType(IPlugin))> _
<ExportMetadata("PluginName", "miniView")> _
<PartCreationPolicy(CreationPolicy.NonShared)> _
Public Class miniViewController
Inherits System.Web.Mvc.Controller
Implements IPlugin
<Import(GetType(HostModel))>
Private m_objHost As HostModel
Public Function Index() As ActionResult
Dim renderedView As ViewResult = View("~/Plugin/miniView.dll/miniView.Index.aspx", m_objHost)
Return renderedView
End Function
This is my plugin view (Index.aspx)
Public Class Index
Inherits Mvc.ViewPage
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim p As ImportData = New ImportData()
Dim strMINILink As String = "http://inside/mini?ContractNumber="
Dim strCUNumber As String = p.instance.IDNumber
Dim strMINIURL As String = strMINILink & strIDNumber
Response.Redirect(strMINIURL)
End Sub
End Class
Public Class ImportData
<Import(GetType(HostModel))>
Property instance As HostModel
Public Sub New()
Dim catalog As AggregateCatalog = New AggregateCatalog()
catalog.Catalogs.Add(New DirectoryCatalog("C:\Documents and Settings\gbv0860\My Documents\cucmConsolidatedView\CUCMCV\cucmConsolidatedView\cucmConsolidatedView\bin"))
Dim _container As CompositionContainer = New CompositionContainer(catalog)
Try
_container.ComposeParts(Me)
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
End Sub
End Class
Any help would be much appreciated! If you need me to clarify anything please let me know! Thanks!
You have put
<Export(GetType(HostModel))>on theHostControllerclass which isn’t actually aHostModel. That’s not a valid export.ASP.NET MVC will request controllers by their exact type, so you should export
HostControllerwith just<Export>.The
<Export(GetType(HostModel))>probably belongs on theHostModelclass. And instead of implementing your own singleton, you should just mark it with<PartCreationPolicy(CreationPolicy.Shared)>.Then there is the fact that you have an import on
ImportData, but that class also isn’t exported and you just create it yourself with anewstatement. Therefore MEF will never see these instances and won’t do anything with the imports.Finally, note that ASP.NET MVC will not automatically use MEF to create objects. You need to register a dependency resolver which wraps a MEF container to set that up. There appears to be a composition provider for APS.NET MVC in MEF2.