Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
CAP
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
tsai
CAP
Commits
d240ed9c
Commit
d240ed9c
authored
Jun 28, 2019
by
Null
Committed by
Savorboard
Jun 28, 2019
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Try to get the service instance using the serviceProvider.GetServices method. (#356)
parent
d7277b1e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
40 additions
and
44 deletions
+40
-44
CAP.ServiceCollectionExtensions.cs
src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs
+3
-22
ConsumerExecutorDescriptor.cs
src/DotNetCore.CAP/ConsumerExecutorDescriptor.cs
+2
-0
IConsumerServiceSelector.Default.cs
src/DotNetCore.CAP/IConsumerServiceSelector.Default.cs
+20
-20
IConsumerInvoker.Default.cs
src/DotNetCore.CAP/Internal/IConsumerInvoker.Default.cs
+15
-2
No files found.
src/DotNetCore.CAP/CAP.ServiceCollectionExtensions.cs
View file @
d240ed9c
...
...
@@ -21,6 +21,7 @@ namespace Microsoft.Extensions.DependencyInjection
/// </summary>
public
static
class
ServiceCollectionExtensions
{
internal
static
IServiceCollection
ServiceCollection
;
/// <summary>
/// Adds and configures the consistence services for the consistency.
/// </summary>
...
...
@@ -34,10 +35,9 @@ namespace Microsoft.Extensions.DependencyInjection
throw
new
ArgumentNullException
(
nameof
(
setupAction
));
}
services
.
TryAddSingleton
<
CapMarkerService
>()
;
ServiceCollection
=
services
;
//Consumer service
AddSubscribeServices
(
services
);
services
.
TryAddSingleton
<
CapMarkerService
>();
//Serializer and model binder
services
.
TryAddSingleton
<
IContentSerializer
,
JsonContentSerializer
>();
...
...
@@ -80,24 +80,5 @@ namespace Microsoft.Extensions.DependencyInjection
return
new
CapBuilder
(
services
);
}
private
static
void
AddSubscribeServices
(
IServiceCollection
services
)
{
var
consumerListenerServices
=
new
List
<
KeyValuePair
<
Type
,
Type
>>();
foreach
(
var
rejectedServices
in
services
)
{
if
(
rejectedServices
.
ImplementationType
!=
null
&&
typeof
(
ICapSubscribe
).
IsAssignableFrom
(
rejectedServices
.
ImplementationType
))
{
consumerListenerServices
.
Add
(
new
KeyValuePair
<
Type
,
Type
>(
typeof
(
ICapSubscribe
),
rejectedServices
.
ImplementationType
));
}
}
foreach
(
var
service
in
consumerListenerServices
)
{
services
.
TryAddEnumerable
(
ServiceDescriptor
.
Transient
(
service
.
Key
,
service
.
Value
));
}
}
}
}
\ No newline at end of file
src/DotNetCore.CAP/ConsumerExecutorDescriptor.cs
View file @
d240ed9c
...
...
@@ -11,6 +11,8 @@ namespace DotNetCore.CAP
/// </summary>
public
class
ConsumerExecutorDescriptor
{
public
TypeInfo
ServiceTypeInfo
{
get
;
set
;
}
public
MethodInfo
MethodInfo
{
get
;
set
;
}
public
TypeInfo
ImplTypeInfo
{
get
;
set
;
}
...
...
src/DotNetCore.CAP/IConsumerServiceSelector.Default.cs
View file @
d240ed9c
...
...
@@ -76,23 +76,21 @@ namespace DotNetCore.CAP
{
var
executorDescriptorList
=
new
List
<
ConsumerExecutorDescriptor
>();
using
(
var
scoped
=
provider
.
CreateScope
())
{
var
scopedProvider
=
scoped
.
ServiceProvider
;
var
consumerServices
=
scopedProvider
.
GetServices
<
ICapSubscribe
>();
foreach
(
var
service
in
consumerServices
)
{
var
typeInfo
=
service
.
GetType
().
GetTypeInfo
();
if
(!
typeof
(
ICapSubscribe
).
GetTypeInfo
().
IsAssignableFrom
(
typeInfo
))
{
continue
;
}
executorDescriptorList
.
AddRange
(
GetTopicAttributesDescription
(
typeInfo
));
var
capSubscribeTypeInfo
=
typeof
(
ICapSubscribe
).
GetTypeInfo
();
foreach
(
var
service
in
ServiceCollectionExtensions
.
ServiceCollection
.
Where
(
o
=>
o
.
ImplementationType
!=
null
&&
o
.
ServiceType
!=
null
))
{
var
typeInfo
=
service
.
ImplementationType
.
GetTypeInfo
();
if
(!
capSubscribeTypeInfo
.
IsAssignableFrom
(
typeInfo
))
{
continue
;
}
return
executorDescriptorList
;
}
var
serviceTypeInfo
=
service
.
ServiceType
.
GetTypeInfo
();
executorDescriptorList
.
AddRange
(
GetTopicAttributesDescription
(
typeInfo
,
serviceTypeInfo
));
}
return
executorDescriptorList
;
}
protected
virtual
IEnumerable
<
ConsumerExecutorDescriptor
>
FindConsumersFromControllerTypes
()
...
...
@@ -112,7 +110,7 @@ namespace DotNetCore.CAP
return
executorDescriptorList
;
}
protected
IEnumerable
<
ConsumerExecutorDescriptor
>
GetTopicAttributesDescription
(
TypeInfo
typeInfo
)
protected
IEnumerable
<
ConsumerExecutorDescriptor
>
GetTopicAttributesDescription
(
TypeInfo
typeInfo
,
TypeInfo
serviceTypeInfo
=
null
)
{
foreach
(
var
method
in
typeInfo
.
DeclaredMethods
)
{
...
...
@@ -135,7 +133,7 @@ namespace DotNetCore.CAP
attr
.
Group
=
attr
.
Group
+
"."
+
_capOptions
.
Version
;
}
yield
return
InitDescriptor
(
attr
,
method
,
typeInfo
);
yield
return
InitDescriptor
(
attr
,
method
,
typeInfo
,
serviceTypeInfo
);
}
}
}
...
...
@@ -143,13 +141,15 @@ namespace DotNetCore.CAP
private
static
ConsumerExecutorDescriptor
InitDescriptor
(
TopicAttribute
attr
,
MethodInfo
methodInfo
,
TypeInfo
implType
)
TypeInfo
implType
,
TypeInfo
serviceTypeInfo
)
{
var
descriptor
=
new
ConsumerExecutorDescriptor
{
Attribute
=
attr
,
MethodInfo
=
methodInfo
,
ImplTypeInfo
=
implType
ImplTypeInfo
=
implType
,
ServiceTypeInfo
=
serviceTypeInfo
};
return
descriptor
;
...
...
src/DotNetCore.CAP/Internal/IConsumerInvoker.Default.cs
View file @
d240ed9c
...
...
@@ -2,6 +2,8 @@
// Licensed under the MIT License. See License.txt in the project root for license information.
using
System
;
using
System.Linq
;
using
System.Reflection
;
using
System.Threading.Tasks
;
using
DotNetCore.CAP.Abstractions
;
using
Microsoft.Extensions.DependencyInjection
;
...
...
@@ -38,9 +40,20 @@ namespace DotNetCore.CAP.Internal
using
(
var
scope
=
_serviceProvider
.
CreateScope
())
{
var
provider
=
scope
.
ServiceProvider
;
var
provider
=
scope
.
ServiceProvider
;
var
serviceType
=
context
.
ConsumerDescriptor
.
ImplTypeInfo
.
AsType
();
var
obj
=
ActivatorUtilities
.
GetServiceOrCreateInstance
(
provider
,
serviceType
);
object
obj
=
null
;
if
(
context
.
ConsumerDescriptor
.
ServiceTypeInfo
!=
null
)
{
obj
=
provider
.
GetServices
(
context
.
ConsumerDescriptor
.
ServiceTypeInfo
.
AsType
())
.
FirstOrDefault
(
o
=>
o
.
GetType
()
==
serviceType
);
}
if
(
obj
==
null
)
{
obj
=
ActivatorUtilities
.
GetServiceOrCreateInstance
(
provider
,
serviceType
);
}
var
jsonContent
=
context
.
DeliverMessage
.
Content
;
var
message
=
_messagePacker
.
UnPack
(
jsonContent
);
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment